From suharto_anggono at yahoo.com  Sun Aug 21 12:44:18 2016
From: suharto_anggono at yahoo.com (Suharto Anggono Suharto Anggono)
Date: Sun, 21 Aug 2016 10:44:18 +0000 (UTC)
Subject: [Rd] 'droplevels' inappropriate change
References: <561464115.479511.1471776258565.ref@mail.yahoo.com>
Message-ID: <561464115.479511.1471776258565@mail.yahoo.com>

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.



[Rd] 'droplevels' inappropriate change
Suharto Anggono Suharto Anggono