我试图制作一个包含两列的表:1)变量名称,2)该变量的级别。
这是数据框的伪代码:
Data <- data.frame(Gender = sample(c("male", "female", "other"),10, replace = TRUE), Age = sample(1:10), Happiness = sample(c("Very","Some","None"), 10, replace = TRUE))
所以本质上我只想要一个表,上面写着:
变量名| 等级
性别| 1 =男,2 =女,3 =其他
年龄| 数字
幸福| 1 =非常,2 =一些,3 =没有
请问我该怎么做?
通过基数R的想法可以是
data.frame(Levels = sapply(Data, function(i){i1 <- levels(i);
ifelse(length(i1) > 0, paste0(seq_along(i1), ' = ', i1, collapse = ' '), class(i))}))
# Levels
#Gender 1 = female 2 = male 3 = other
#Age integer
#Happiness 1 = None 2 = Some 3 = Very
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句