
上QQ阅读APP看书,第一时间看更新
6-6 再看levels参数
对于任何因子而言,我们都可以使用str()函数查看此因子的结构。例如,参考fifth.factor,如下所示。

由上述执行结果可知,fifth.factor因子有4个Levels的值,分别是“East”“West”……这些值对应的整数分别是1, 2, 3, 4。
对于任何因子而言,如果查看它的Levels,均可以使用levels()函数。
实例ch6_9:使用levels()函数,了解fifth.factor的Levels。

nlevels()函数可传回levels的数量。
实例ch6_10:使用nlevel()函数,了解fifth.factor的Levels的数量。

由上述执行结果可知,nlevels()传回的是一个数值向量,此数值代表Levels的数量。length()则可传回因子元素的数量。
实例ch6_11:使用length()函数传回fifth.factor的元素数量。如果length()函数参数放的是levels(fifth.factor),则可传回Levels的数量。

R语言也允许levels()函数配合索引使用,只取Levels的部分内容。
实例ch6_12:只取fifth.factor的levels的后3个。
