繁体   English   中英

具有比R中的观察更多级别的因子的数据帧

[英]Dataframe with a Factor that has More Levels than Observations in R

奇怪的问题。

如何在因子变量中具有比观察值更多级别的数据帧?

> str(cash.SR)
'data.frame':   50 obs. of  2 variables:
 $ state     : Factor w/ 55 levels "Alabama","Alaska",..: 1 45 46 15 12 36 4 44 18 55 ...
 $ cash_avail: int  14909657 11401584 5900970 5760263 5612664 5466801 5435156 4850103 4415570 3459859 ...

这并不复杂。 我有一个带有2列和50行的df(一个对应于每个状态)。

谢谢。

创建具有更多级别而非值的因子非常容易:

 tfac <- factor(1, levels=1:3)
 dput(tfac)
     #structure(1L, .Label = c("1", "2", "3"), class = "factor")

如果从具有55个级别的因子变量中进行子集并且仅获得50个命中,则级别的长度不会自动缩小。 您需要使用drop.levels返回具有较少级别的因子对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM