list 如何在 R 中将因子水平转换为列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30140304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:07:37 来源:igfitidea点击:
How to convert factor levels to list, in R
提问by jpinelo
Imagine a data frame such as df1 below:
想象一个数据框,例如下面的 df1:
df1 <- data.frame(v1 = as.factor(c("m0p1", "m5p30", "m11p20", "m59p60", "m59p60")))
How do I create a list of all the levels of a variable? Thank you.
如何创建变量所有级别的列表?谢谢你。
回答by Phil
To print the levels in the variable, use levels()
as @scoa says:
要打印变量中的级别,请使用levels()
@scoa 说的:
levels(df1$v1)
To make it an explicit list use as.list()
as well:
要使其成为显式列表as.list()
,还可以使用:
l <- as.list(levels(df1$v1))
l
回答by Shashank Raina
This converts the factor to something manageable:
这将因素转换为可管理的:
df1$v1 <- vapply(df1$v1, paste, collapse = ", ", character(1L))