[英]convert list with vectors of different length into data frame
I'm trying to convert a list containing multiple vectors of different length (of type factor) to a dataframe that has equal numbers of rows as elements in list and only one column. 我正在尝试将包含不同长度(类型因子)的多个向量的列表转换为具有相同行数作为列表中的元素且仅有一列的数据帧。
Example: 例:
ex.list <- (list(c("Tom", "Ron", "Joe"), c("Ron", "Joe"), c("Tom")))
ex.list <- lapply(ex.list, function(x) as.factor(x))
Desired output would be: 期望的输出将是:
ex.list
col1
#1 Tom, Ron, Joe
#2 Ron, Joe
#3 Tom
When I call str(ex.list$col1)
I'd like to get: 当我调用
str(ex.list$col1)
我想得到:
Factor w/3 levels: "Tom, Ron, Joe"
I don't have trouble to convert ex.list
in a dataframe with 3 rows and 3 columns, but that isn't what I want. 我没有遇到在3行3列的数据帧中转换
ex.list
问题,但这不是我想要的。
What's the best approach here? 这里最好的方法是什么? Should I
unlist
and sequece the vector somehow into this 3 row, 1 column data frame? 我应该以某种方式将向量
unlist
并将其unlist
到这3行1列数据帧中吗? is there a handy function to do that? 有没有方便的功能呢?
df = data.frame(col1 = unlist(lapply(ex.list, paste, collapse = ", ")))
> df
col1
1 Tom, Ron, Joe
2 Ron, Joe
3 Tom
To get the unique names you'd have to run something like this 要获得唯一的名称,您必须运行这样的东西
> unique(unlist(strsplit(as.character(df[ , 1]), ", ")))
[1] "Tom" "Ron" "Joe"
You're not going to be able to look at the levels of the factor with the way you want your data.frame setup. 您无法以您希望data.frame设置的方式查看因子的级别。
You can use list columns with tidyr::nest
and tidyr::unnest
: 您可以将列表列与
tidyr::nest
和tidyr::unnest
:
library(tidyverse)
df <- data.frame(id=seq_along(ex.list), col1 = unlist(lapply(ex.list, paste, collapse = ","))) %>%
mutate(col1 = stringr::str_split(col1, ",")) %>%
unnest %>%
mutate(col1=as.factor(col1)) %>%
nest(col1)
str(df)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 2 variables:
$ id : int 1 2 3
$ data:List of 3
..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of 1 variable:
.. ..$ col1: Factor w/ 3 levels "Joe","Ron","Tom": 3 2 1
..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 2 obs. of 1 variable:
.. ..$ col1: Factor w/ 3 levels "Joe","Ron","Tom": 2 1
..$ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 1 obs. of 1 variable:
.. ..$ col1: Factor w/ 3 levels "Joe","Ron","Tom": 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.