[R] List seems to drop empty levels of factors when containing them

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Wed Nov 10 05:18:43 CET 2004



Andrew Robinson wrote:
> Greetings R community,
> 
> I am curious about the following behaviour: if I define a factor, and
> then store a subset of it in a list, the stored version seems to drop
> levels that were not included in the subset.  E.g. ..
> 
> 
>>mask <- c(T, F)
>>grp.1 <- factor(c(1,2))
>>list(grp.1)
> 
> [[1]]
> [1] 1 2
> Levels: 1 2
> 
> 
>>list(grp.1[mask])
> 
> [[1]]
> [1] 1
> Levels: 1
> 
> It is as though the list were redefining the factor and dropping
> empty levels.  I would like to keep them (I am using the list for a
> two-dimensional tapply). Is there any way to avoid this?
> 
> I'm using R 2.0.0 on FreeBSD 5.2.1.
> 
> Thanks,
> 
> Andrew

Andrew,

I cannot replicate this with R-2.0.0 patched on Win2000Pro:


 > mask <- c(T, F)
 > grp.1 <- factor(c(1,2))
 > list(grp.1)
[[1]]
[1] 1 2
Levels: 1 2

 > grp.1[mask]
[1] 1
Levels: 1 2
 > list(grp.1[mask])
[[1]]
[1] 1
Levels: 1 2

--sundar




More information about the R-help mailing list