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
The 'levels' is supposed to be the order in which you want the factors to be. You have specified levels=1:20 and there are no matching values in letters[1:20], therefore NAs:> x <- factor(letters[1:20], levels=letters[20:1]) > x[1] a b c d e f g h i j k l m n o p q r s t Levels: t s r q p o n m l k j i h g f e d c b a> x <- factor(letters[1:20], levels=LETTERS[20:1]) # error no matching levels > x[1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> [20] <NA> Levels: T S R Q P O N M L K J I H G F E D C B A> x <- factor(letters[1:20]) > x[1] a b c d e f g h i j k l m n o p q r s t Levels: a b c d e f g h i j k l m n o p q r s t> as.integer(x)[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20>You can see in the last statement that the integer values for x are numbered, probably as you were expecting them. On Sat, Aug 2, 2008 at 5:48 PM, <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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
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.