我有一个数据框架,其中包含列表内.

df <- data.frame(
      id=c(1:4),
      a=I(list(c(1,"a1"),2,c("a31","a32","a33"),"a4")),
      b=I(list(2,c("b1","b2",3),c("b3","b4"),4))
    ); print(df)

  id            a         b
1  1        1, a1         2
2  2            2 b1, b2, 3
3  3 a31, a32....    b3, b4
4  4           a4         4

现在,我需要unnest个列表才能获得这样的数据帧:

df2 <- data.frame(
      id=c(1,1,2,2,2,3,3,3,3,3,3,4),
      a=c(1,"a1",2,2,2,"a31","a31","a32","a32","a33","a33","a4"),
      b=c(2,2,"b1","b2",3,"b3","b3","b3","b4","b4","b4",4)
    ) ; print(df2)

   id   a  b
1   1   1  2
2   1  a1  2
3   2   2 b1
4   2   2 b2
5   2   2  3
6   3 a31 b3
7   3 a31 b3
8   3 a32 b3
9   3 a32 b4
10  3 a33 b4
11  3 a33 b4
12  4  a4  4

我过go 用unnest()表示某些行/列中包含相同数量的列表元素,但当前数据框在某些行/列中包含不同数量的元素.目前,我面临以下错误.

> target <- c("id","a","b")
> df %>% unnest(cols=target)
Error in `unnest()`:
! In row 3, can't recycle input of size 3 to size 2.
Run `rlang::last_trace()` to see where the error occurred.

由于where it happens(行/列)和how many elements it will contain的不可预测性,我找不到适当的方法来处理这个问题.列数及其名称不能预先确定.

我很欣赏你的建议,特别是简单的建议,可以在dplyr年内整合到目前的管道运营中.Base R和其他方法也是受欢迎的.

====

Rreproducible example data

让我分享一个我正在处理的实际数据帧的可复制示例.它不适用于@ JumasIsCoding的解决方案.我怀疑它是由NULL引起的,但这不是原因.空白/空白不一定重复.

structure(list(cluster = c("1", "2", "3", "4", "5", "6"), st_sub_main_th = list(
    "hira", NULL, "tsuma", "tsuma", NULL, c("other", "hira")), 
    roo_main = list("2", "4", "3", "2", c("1", "3"), c("6", "7", 
    "2", "1")), st_con_rt = list("sub-room", "main-room", "sub-room", 
        "sub-room", "main-room", "sub-room"), st_con_tr = list(
        "terrace", c("terrace", "direct"), "terrace", "terrace", 
        "terrace", "direct"), st_adsb = list("add", "add", "add", 
        "sub", "add", "sub"), st_th = list(NULL, "tsuma", NULL, 
        NULL, "hira", NULL), st_sub2_main_th = list(NULL, NULL, 
        NULL, "hira", "hira", "tsuma"), isstilt = list(NULL, 
        NULL, NULL, NULL, NULL, "0")), class = "data.frame", row.names = c(NA, 
-6L))

推荐答案

使用tidyverse:

library(tidyverse)
exec(pmap_df, df, expand_grid)

# A tibble: 15 × 9
   cluster st_sub_main_th roo_main st_con_rt st_con_tr st_adsb st_th st_sub2_main_th isstilt
   <chr>   <chr>          <chr>    <chr>     <chr>     <chr>   <chr> <chr>           <chr>  
 1 1       hira           2        sub-room  terrace   add     NA    NA              NA     
 2 2       NA             4        main-room terrace   add     tsuma NA              NA     
 3 2       NA             4        main-room direct    add     tsuma NA              NA     
 4 3       tsuma          3        sub-room  terrace   add     NA    NA              NA     
 5 4       tsuma          2        sub-room  terrace   sub     NA    hira            NA     
 6 5       NA             1        main-room terrace   add     hira  hira            NA     
 7 5       NA             3        main-room terrace   add     hira  hira            NA     
 8 6       other          6        sub-room  direct    sub     NA    tsuma           0      
 9 6       other          7        sub-room  direct    sub     NA    tsuma           0      
10 6       other          2        sub-room  direct    sub     NA    tsuma           0      
11 6       other          1        sub-room  direct    sub     NA    tsuma           0      
12 6       hira           6        sub-room  direct    sub     NA    tsuma           0      
13 6       hira           7        sub-room  direct    sub     NA    tsuma           0      
14 6       hira           2        sub-room  direct    sub     NA    tsuma           0      
15 6       hira           1        sub-room  direct    sub     NA    tsuma           0  

请注意,上述内容相当于:

bind_rows(exec(pmap, df, expand_grid))

如果您有混合数据库,请考虑以下几点:

exec(rbind, !!!exec(pmap, df, expand_grid))

R相关问答推荐

使用ggcorrplot在相关性矩阵上标注supertitle和index标签

从多个前置日期中获取最长日期

使用gggrassure减少地块之间的空间

R:连接值,而不是变量?

将数字转换为分钟和秒

R中的时间序列(Ts)函数计数不正确

如何读取CSV的特定列时,给定标题作为向量

如何在R forestplot中为多条垂直线分配唯一的 colored颜色 ?

如何删除最后一个可操作对象

如何删除仅在数据集顶部和底部包含零的行

如何将一列中的值拆分到R中各自的列中

如何在R中改变fviz_pca_biplot中圆的边界线的 colored颜色 ?

根据r中另一个文本列中给定的范围对各列求和

通过R:文件名未正确写入[已解决]将.nc文件转换和导出为.tif文件

R-使用stri_trans_General()将其音译为德语字母

如何使用grepl()在数据帧列表中 Select 特定字符串?

如何使投篮在R中保持一致

R:如何在数据集中使用Apply

如何在一种 colored颜色 中设置数值变量的 colored颜色 和高于阈值的 colored颜色 点?

带有Bootswatch Cerulean主题的shiny 仪表板中的浏览&按钮可见性问题