[Rd] 'droplevels' inappropriate change

Suharto Anggono Suharto Anggono suharto_anggono at yahoo.com
Sun Aug 21 12:44:18 CEST 2016

In R devel r71124, if 'x' is a factor, droplevels(x) gives
factor(x, exclude = NULL) .
In R 3.3.1, it gives
factor(x) .

If a factor 'x' has NA and levels of 'x' doesn't contain NA, factor(x) gives the expected result for droplevels(x) , but factor(x, exclude = NULL) doesn't. As I said in https://stat.ethz.ch/pipermail/r-devel/2016-May/072796.html , factor(x, exclude = NULL) adds NA as a level.

factor(x, exclude = if(anyNA(levels(x))) NULL else NA ) ,
like in the code of function `[.factor` (in the same file, factor.R, as 'droplevels'), is better.
It is possible just to use
x[, drop = TRUE] .

For a factor 'x' that has NA level and also NA value, factor(x, exclude = NULL) is not perfect, though. It change NA to be associated with NA factor level.

More information about the R-devel mailing list