Re: [R] initialize a factor vector

From: Gavin Simpson <>
Date: Sun, 13 Jul 2008 19:18:37 +0100

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

HTH G mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Sun 13 Jul 2008 - 18:23:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 19 Jul 2008 - 14:31:55 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive