【发布时间】:2020-08-26 15:01:54
【问题描述】:
这是一个data.frame,其中第二列是一列列表(注意还有一个NULL)。
我们如何将每个列表转换为常规元素,以便该列与任何其他字符类列一样? (NULL 可以是NA)
df <- structure(list(Year = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2013L,
2014L, 2014L, 2014L, 2014L, 2014L), Country = list(Country = "Canada",
Country = "Germany", Country = "France", Country = "Germany",
Country = "Mexico", Country = "Germany", Country = "Germany",
Country = "Canada", NULL, Country = "Germany", Country = "Mexico",
Country = "Canada", Country = "Mexico", Country = "Germany",
Country = "Canada", Country = "United States of America",
Country = "Canada", Country = "Mexico", Country = "Canada",
Country = "Germany")), class = "data.frame", row.names = c(NA,
-20L))
注意
df %>% sapply(class)
Year Country
"integer" "list"
想要的结果:
- 数据相同,但
df %>% sapply(class)
Year Country
"integer" "character"
【问题讨论】: