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.

Using
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.

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to