目前,我正在尝试弄清楚如何在取消嵌套时将内部列表和其他列表的名称保持在 tibble 中。
该函数的.id
参数unnest
是我找到的最接近的参数,但它开始对值进行编号而不是使用给定的名称。
这是我对最终小标题的想法的MWE:
library(dplyr)
library(tidyr)
df.1 <- tibble(
x = list("Foo","Bar"),
y = list(
list(a = list(aa = 1, ab = 2), b = list(ba = 6, bb = 22)),
list(c = list(ca = 561, cb = 35), d = list(da = 346, db = 17))
)
)
df.2 <- unnest(df.1, .id = "name.outher")
df.3 <- unnest(df.2, .id = "name.inner")
# How do I get from this:
#
#-----------------------------------------------------------------------
# x | y |
#-----+----------------------------------------------------------------+
# Foo | list(a = list(aa = 1, ab = 2), b = list(ba = 6, bb = 22)) |
#-----+----------------------------------------------------------------+
# Bar | list(c = list(ca = 561, cb = 35), d = list(da = 346, db = 17)) |
#-----------------------------------------------------------------------
#
# to this:
#
#---------------------------------------
# x | name.outher | y | name.inner |
#-----+-------------+-----+------------+
# Foo | a | 1 | aa |
#-----+-------------+-----+------------+
# Foo | a | 2 | ab |
#-----+-------------+-----+------------+
# Foo | b | 6 | ba |
#-----+-------------+-----+------------+
# Foo | b | 22 | bb |
#-----+-------------+-----+------------+
# Bar | c | 561 | ca |
#-----+-------------+-----+------------+
# Bar | c | 35 | cb |
#-----+-------------+-----+------------+
# Bar | d | 346 | da |
#-----+-------------+-----+------------+
# Bar | d | 17 | db |
#-------------------------------------
#
# instead of this:
#
#---------------------------------------
# x | name.outher | y | name.inner |
#-----+-------------+-----+------------+
# Foo | 1 | 1 | 1 |
#-----+-------------+-----+------------+
# Foo | 1 | 2 | 1 |
#-----+-------------+-----+------------+
# Foo | 1 | 6 | 2 |
#-----+-------------+-----+------------+
# Foo | 1 | 22 | 2 |
#-----+-------------+-----+------------+
# Bar | 2 | 561 | 3 |
#-----+-------------+-----+------------+
# Bar | 2 | 35 | 3 |
#-----+-------------+-----+------------+
# Bar | 2 | 346 | 4 |
#-----+-------------+-----+------------+
# Bar | 2 | 17 | 4 |
#---------------------------------------
您知道如何在取消嵌套此数据结构的同时保留名称吗?