[R] number of levels for a factor

Petr Pikal petr.pikal at precheza.cz
Fri Mar 2 09:22:34 CET 2007



On 1 Mar 2007 at 12:17, Aimin Yan wrote:

Date sent:      	Thu, 01 Mar 2007 12:17:09 -0600
To:             	r-help at stat.math.ethz.ch
From:           	Aimin Yan <aiminy at iastate.edu>
Subject:        	[R] number of levels for a factor

> I have temp list which have 19 data.frame
> I want to get number of levels for pr in the first dat.frame
> I do this like this:
> temp[[1]]$pr just has "1A24"
> after I do nlevels(temp[[1]]$pr)
> I expect to get 1, but I get 19
> 
> anyone know why?

Hi

help page for [.factor tells you that using subsetting results in

A factor with the same set of levels as x unless drop=TRUE.

I presume your list resulted from some splitting operation and that 
original set of levels was preserved.

HTH
Petr


> 
>  > tail(temp[[1]]$pr)
> [1] 1A24 1A24 1A24 1A24 1A24 1A24
> 19 Levels: 1A24 1A57 1A5J 1A6X 1AB7 1AF8 1AFI 1AGG 1AH9 1AHL 1AJ3 1AJW
> ... 1AZK
>  > nlevels(temp[[1]]$pr)
> [1] 19
> 
> Aimin
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.

Petr Pikal
petr.pikal at precheza.cz



More information about the R-help mailing list