[R] Basic factor question.

Erik Iverson iverson at biostat.wisc.edu
Sun Aug 3 00:29:58 CEST 2008


Kevin -

Read more closely "levels", being an optional vector of the values x 
might have taken.  You are saying x might have taken 1:20, and then 
giving it the first 20 letters, which are not part of "the values x 
might have taken".

Try:

x <- factor(letters[1:20])
levels(x)
x

vs.

y <- factor(letters[1:20], levels = letters)
levels(y)
y

vs.

z <- factor(letters[1:20], levels = letters[1:19])
levels(z)
z

That might help show you what's going on?

Best,
Erik Iverson

rkevinburton at charter.net wrote:
> Doing ?factor I get:
> 
> x a vector of data, usually taking a small number of distinct values. 
> levels an optional vector of the values that x might have taken. The default is the set of values taken by x, sorted into increasing order. 
> 
> So if I do:
> 
> factor(letters[1:20],level=seq(1:20)
>  [1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
> [16] <NA> <NA> <NA> <NA> <NA>
> Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> 
> So why all of the NA? What happend to 'a'. 'b', etc.? I was expecting a=1, b=2, c=3 etc.
> 
> I am missing something. Please help with my understanding. 
> 
> Keviin
> 
> ______________________________________________
> 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.



More information about the R-help mailing list