【发布时间】:2022-11-22 17:04:06
【问题描述】:
我有两个 data.tables 如下:
DT_1 <- data.table(Type = c("A1","B1","A1","B1"))
DT_2 <- data.table(A1 = c(1,2,3),A1_ref = c(4,5,6),
B1 = c(11,12,13,14),B1_ref = c(15,16,17,18))
在 DT_1 中将“A1”作为“类型”的地方,我想引入(作为单个嵌套数据表)以“A1”开头的 DT_2 列,即“A1”和“A1_ref”。
同样,在 DT_1 中我将“B1”作为“类型”的地方,我想跨过以“B1”开头的相应列,即“B1”和“B1_ref”。
预期的输出将是以下结构,其中我将 DT_2 中的数据表嵌套在 DT_1 中名为“Ref_table”的新列中:
DT_1 <- data.table(Type = c("A1","B1","A1","B1"),
Ref_table = c(DT_3,DT_4,DT_3,DT_4))
在哪里:
DT_3 <- data.table(A1 = c(1,2,3),A1_ref = c(4,5,6)) # and
DT_4 <- data.table(B1 = c(1,2,3),B1_ref = c(4,5,6))
到目前为止,我试图将(来自 Tidyr 的)DT_2 融化/“收集”成长格式,然后与 DT_1 进行“on”连接,但不幸的是,这并没有给我带来什么。 我目前对我可以采取的其他方法感到困惑。
任何帮助将不胜感激!
谢谢, 菲尔
【问题讨论】:
标签: r data.table tidyr