[R] Levels of a factor

David Winsemius dwinsemius at comcast.net
Wed Jul 24 20:35:55 CEST 2013

On Jul 24, 2013, at 6:25 AM, Borja Rivier wrote:

> 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?

When you take a subset of a factor vector, the levels are not reduced to the unique values in the new vector. There is droplevels function that would need to be applied if you already have such a vector, and there is a drop argument that you need to set to TRUE in the `[.factors` call if you want to "attack the problem at the source".



David Winsemius
Alameda, CA, USA

More information about the R-help mailing list