G'day Peter, On Wed, 12 Nov 2008 00:42:36 +0100 Peter Dalgaard <[EMAIL PROTECTED]> wrote:
> > On 12/11/2008, at 11:29 AM, Peter Dalgaard wrote: > >> > >> Not that one again! For at least one other value of one, the > >> expectation is the opposite: Factor levels do not go away just > >> because they happen not to be present in data. > >> > >> fct <- lapply(dd, is.factor) > >> dd[fct] <- lapply(dd[fct], "[", drop=TRUE) > >> > (Actually, the last line could have had lapply(dd[fct],factor), I > just got confused about whether in would preserve the level order.) That was my first thought too, that lapply(dd[fct], factor) should be enough. But then I thought that ordered factors test TRUE for is.factor and that using factor() on an ordered factor will not only remove the factor levels that are missing but also remove the information on the ordering of the remaining levels. A short test showed that this is not the case; and after reading the help page of factor() I realised that this is due to the design of the function. So perhaps this example should be documented as a case in which the design decisions of the R developer save a naive user of accidentally doing a wrong thing (namely turning an ordinal variable into a nominal). :) Cheers, Berwin ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.