[R] initialize a factor vector

Gavin Simpson gavin.simpson at ucl.ac.uk
Sun Jul 13 20:18:37 CEST 2008

On Sun, 2008-07-13 at 18:47 +0200, Johannes Huesing wrote:
> What is the least surprising way of initializing a factor with 
> predefined levels and with length 0? 
> as.factor(c("eins", "zwei", "drei"))[FALSE] 
> does the job but looks a bit weird.

Notice that one does not need to specify any data as argument 'x' to
factor() because, by default, x = character(). Therefore, we need only
specify the levels we want:

> factor(levels = c("one","two","three"))
Levels: one two three



More information about the R-help mailing list