我希望将data.table::melt()
任意嵌套的列表my_list <- list(
"first_node" = list(
"group_a" = list(
"E001" = 1:5,
"E002" = list(
"F001" = 6:10,
"F002" = 11:15
)
),
"group_b" = list(
"XY01" = list(
"Z1" = LETTERS[1:5],
"Z2" = LETTERS[6:10]
),
"YZ" = LETTERS[11:15]
),
"group_c" = list(
"QQQQ" = list(
"RRRR" = 200:300
)
)
),
"second_node" = list(
"group_d" = list(
"L1" = 99:101,
"L2" = 12
)
)
)
保留到一个数据帧,同时为每个嵌套级别维护一个单独的行。例如:
my_df <- data.table::melt(my_list)
使用> my_df
value L4 L3 L2 L1
1 1 <NA> E001 group_a first_node
2 2 <NA> E001 group_a first_node
3 3 <NA> E001 group_a first_node
4 4 <NA> E001 group_a first_node
5 5 <NA> E001 group_a first_node
6 6 F001 E002 group_a first_node
7 7 F001 E002 group_a first_node
8 8 F001 E002 group_a first_node
9 9 F001 E002 group_a first_node
10 10 F001 E002 group_a first_node
11 11 F002 E002 group_a first_node
12 12 F002 E002 group_a first_node
13 13 F002 E002 group_a first_node
14 14 F002 E002 group_a first_node
15 15 F002 E002 group_a first_node
16 1 Z1 XY01 group_b first_node
17 2 Z1 XY01 group_b first_node
[... and so on...]
给我们:
> desired_df
value L4 L3 L2 L1
1 <NA> <NA> <NA> <NA> first_node # individual row for first level
2 <NA> <NA> <NA> group_a first_node # individual row for second level
3 <NA> <NA> E001 group_a first_node # individual row for third level
4 2 <NA> E001 group_a first_node # value-levels remain the same
5 3 <NA> E001 group_a first_node
6 4 <NA> E001 group_a first_node
7 5 <NA> E001 group_a first_node
8 <NA> F001 E002 group_a first_node # individual row for third level
9 6 F001 E002 group_a first_node
10 7 F001 E002 group_a first_node
11 8 F001 E002 group_a first_node
[... and so on...]
鉴于此,我想将每个嵌套级别保留为额外的一行(显然缺少值)。
所需的输出:
data.table::melt()
我可以使用g++-9 -std=c++2a -fconcepts
完成此操作吗?谢谢!