将不同长度向量的列表转换为 `tibble`
Turn list of different length vectors into a `tibble`
我目前有一个不同长度的字符向量列表。像这样:
list(
c('this','is','first'),
c('this','is','second','it','longer'),
c('this is a list','that is length 2')
)
我想将列表中每个向量的所有元素组合成 tibble
中的一行。像这样:
data_frame(column_1 =
c('this is first',
'this is second it longer',
'this is a list that is length 2'))
如果可能的话,我想使用基础 R 或 tidyverse
中的包。
您可以使用 purrr
和 stringr
x <- list(
c('this','is','first'),
c('this','is','second','it','longer'),
c('this is a list','that is length 2')
)
tibble(column1= map_chr(x, str_flatten, " "))
请注意 str_flatten
是 stringr_1.3.0
的新手
这也可以使用基本 R 轻松完成(没有 tidyverse 函数)
data.frame(column1 = sapply(x, paste, collapse= " "))
我目前有一个不同长度的字符向量列表。像这样:
list(
c('this','is','first'),
c('this','is','second','it','longer'),
c('this is a list','that is length 2')
)
我想将列表中每个向量的所有元素组合成 tibble
中的一行。像这样:
data_frame(column_1 =
c('this is first',
'this is second it longer',
'this is a list that is length 2'))
如果可能的话,我想使用基础 R 或 tidyverse
中的包。
您可以使用 purrr
和 stringr
x <- list(
c('this','is','first'),
c('this','is','second','it','longer'),
c('this is a list','that is length 2')
)
tibble(column1= map_chr(x, str_flatten, " "))
请注意 str_flatten
是 stringr_1.3.0
这也可以使用基本 R 轻松完成(没有 tidyverse 函数)
data.frame(column1 = sapply(x, paste, collapse= " "))