简体   繁体   中英

How to convert factor levels to list, in R

Imagine a data frame such as df1 below:

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.

To print the levels in the variable, use levels() as @scoa says:

levels(df1$v1)

To make it an explicit list use as.list() as well:

l <- as.list(levels(df1$v1))
l

这会将因子转换为可管理的因素:

df1$v1 <- vapply(df1$v1, paste, collapse = ", ", character(1L))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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