在 R 中使用 data.table 取消嵌套多列

Un-nesting multiple columns with data.table in R

我正在尝试为具有多个嵌套列的 data.table 找到 tidyr::unnest() 的等价物:

MT <- as.data.table(mtcars)

MT_NEST_MULT <- MT[, .(data1 = .(.SD[, .(mpg, hp)]), data2 = .(.SD[, !c("mpg", "hp")])), by = .(cyl, gear)]
cyl gear data1              data2
8   3    <S3: data.table>   <S3: data.table>    
8   5    <S3: data.table>   <S3: data.table>    
6   4    <S3: data.table>   <S3: data.table>    
6   3    <S3: data.table>   <S3: data.table>    
6   5    <S3: data.table>   <S3: data.table>    
4   4    <S3: data.table>   <S3: data.table>    
4   3    <S3: data.table>   <S3: data.table>    
4   5    <S3: data.table>   <S3: data.table>    

取消嵌套单个列很容易:MT_NEST_MULT[, rbindlist(data1), by = .(cyl, gear)]

但我不知道如何取消嵌套两者,即做相当于 tidyr::unnest(..., c(data1, data2))

这是一个选项 - 我们指定要应用 rbindlist.SDcols 的列,遍历 .SD(Data.table 的子集),应用 rbindlist 并用 c

展平输出
library(data.table)
MT_NEST_MULT[, do.call(c, unname(lapply(.SD, rbindlist))), 
      .SDcols = patterns('data'), by = .(cyl, gear)]

-输出

    cyl  gear   mpg    hp  disp  drat    wt  qsec    vs    am  carb
    <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
 1:     6     4  21.0   110 160.0  3.90 2.620 16.46     0     1     4
 2:     6     4  21.0   110 160.0  3.90 2.875 17.02     0     1     4
 3:     6     4  19.2   123 167.6  3.92 3.440 18.30     1     0     4
 4:     6     4  17.8   123 167.6  3.92 3.440 18.90     1     0     4
 5:     4     4  22.8    93 108.0  3.85 2.320 18.61     1     1     1
 6:     4     4  24.4    62 146.7  3.69 3.190 20.00     1     0     2
 7:     4     4  22.8    95 140.8  3.92 3.150 22.90     1     0     2
 8:     4     4  32.4    66  78.7  4.08 2.200 19.47     1     1     1
 9:     4     4  30.4    52  75.7  4.93 1.615 18.52     1     1     2
10:     4     4  33.9    65  71.1  4.22 1.835 19.90     1     1     1
...