【问题标题】:Turn list of different length vectors into a `tibble`将不同长度向量的列表转换为“tibble”
【发布时间】:2018-02-22 21:30:47
【问题描述】:

我目前有一个不同长度的字符向量列表。像这样:

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 中的软件包。

【问题讨论】:

    标签: r list purrr tibble


    【解决方案1】:

    您可以使用purrrstringr

    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_flattenstringr_1.3.0 的新成员

    这也可以通过基本 R 轻松完成(没有 tidyverse 函数)

    data.frame(column1 = sapply(x, paste, collapse= " "))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-31
      • 2013-04-07
      • 2017-01-07
      • 1970-01-01
      • 2020-08-02
      • 2020-02-18
      • 1970-01-01
      • 2020-06-08
      相关资源
      最近更新 更多