[Rd] Should mcnemar.test use as.factor instead of factor

Greg Snow Greg.Snow at imail.org
Tue Apr 27 18:54:13 CEST 2010


I am working with the mcnemar.test function and the help does not show a maintainer/author, but it is part of the stats package.

My issue is that I want to use the test on 2 variables with possible values of 0:3, in one of the tests one of the variables does not have any 3's, so to make sure that the matrix is square I do:

> x <- factor(x, levels=0:3)
> y <- factor(y, levels=0:3)

If I run mcnemar.test on the table of x and y then everything works fine, but if I pass in x and y without running table first then I get an error about both variables needing to have the same number of levels (which they did when I passed them in).  The problem occurs because the function when handed the raw data does its own conversion to factor using the factor function which drops the unused level in the one variable.  A simple fix should be to replace the call to factor with a call to as.factor (which will not change anything for variables that are already factors and therefore not drop levels).  I cannot imagine any code that would break from this change, but that could just be a lack of imagination on my part.

So, can anyone think of a reason not to change factor to as.factor?

Is this worth a bug report/enhancement request?



> sessionInfo()
R version 2.11.0 (2010-04-22) 
i386-pc-mingw32 

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] tools_2.11.0

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111



More information about the R-devel mailing list