[英]R: Mapping table of levels and labels of factor variable
I am pretty sure I am missing something here, but how can I get a mapping table of the integer codes and the labels of a factor variable in R? 我很确定我在这里遗漏了一些东西,但是如何获得R中整数代码和因子变量的标签的映射表?
For example, in the chickwts
dataset, I would like the output for the feed
variable 例如,在
chickwts
数据集中,我想要feed
变量的输出
1 --> casein
2 --> horsebean
3 --> linseed
4 --> meatmeal
5 --> soybean
6 --> sunflower
I am pretty sure there is a built-in function for this, but I can't find it, and neither levels
, nlevels
or unclass
gives me what I want. 我敢肯定有这个内置的功能,但我不能找到它,既不
levels
, nlevels
或unclass
给我我想要的。
Any suggestions? 有什么建议么?
The codes are just the index into the levels(...)
vector. 这些代码只是进入
levels(...)
向量的索引。
with(chickwts,data.frame(code=seq_along(levels(feed)),levels=levels(feed)))
# code levels
# 1 1 casein
# 2 2 horsebean
# 3 3 linseed
# 4 4 meatmeal
# 5 5 soybean
# 6 6 sunflower
This is the same result you get with as.integer(...)
. 这与使用
as.integer(...)
获得的结果相同。
with(chickwts,data.frame(code=as.numeric(unique(feed)),level=unique(feed)))
# code level
# 1 2 horsebean
# 2 3 linseed
# 3 5 soybean
# 4 6 sunflower
# 5 4 meatmeal
# 6 1 casein
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.