Re: [R] initialize a factor vector

From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>
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"))
factor(0)
Levels: one two three

HTH G



R-help_at_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. 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 https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive