[R] Levels of a factor

arun smartpink111 at yahoo.com
Wed Jul 24 15:39:47 CEST 2013


Hi,
 vec1<- factor(1:5,levels=1:10)
 vec1
#[1] 1 2 3 4 5
#Levels: 1 2 3 4 5 6 7 8 9 10


vec2<-droplevels(vec1)
 levels(vec2)
#[1] "1" "2" "3" "4" "5"
 vec2
#[1] 1 2 3 4 5
#Levels: 1 2 3 4 5
A.K.

Hi all, 

I am having a bit of trouble using the levels() function. 
I have a factor with many elements, and when I use the function 
levels() to extract the list of unique elements, some of the elements 
returned are not actually in the factor. 

For example I would have this: 

> vector <- dataset$Benchmark 
> class(vector) 
[1] "factor" 
> length(vector) 
[1] 35615 
> vector2 <- levels(vector) 
> length(which(!(vector2 %in% vector))) 
[1] 235 

Does anyone know how this is possible? 

Many thanks! 

Borja



More information about the R-help mailing list