【发布时间】:2017-10-22 04:25:38
【问题描述】:
假设我有下表:
Text ID Prior ID
Text1 1 1
Text2 1 2
Text3 1 3
Text4 2 4
我的目标:对于数据集中所有相同的 ID,分配相同的 Text 值。例如,在这种情况下,我想将 Text2 和 Text3 更改为 Text1,因为 ID 列中的值与 Prior ID 中的值相同。
【问题讨论】:
标签: r
假设我有下表:
Text ID Prior ID
Text1 1 1
Text2 1 2
Text3 1 3
Text4 2 4
我的目标:对于数据集中所有相同的 ID,分配相同的 Text 值。例如,在这种情况下,我想将 Text2 和 Text3 更改为 Text1,因为 ID 列中的值与 Prior ID 中的值相同。
【问题讨论】:
标签: r
使用data.table的解决方案
library(data.table)
# Original table should be named d
setDT(d)
res <- merge(d, d[ID == `Prior ID`], "ID", all.x = TRUE)[is.na(Text.y), Text.y := Text.x]
res <- res[, .(Text = Text.y, ID, `Prior ID` = `Prior ID.x`)]
res
Text ID Prior ID
1: Text1 1 1
2: Text1 1 2
3: Text1 1 3
4: Text4 2 4
【讨论】:
head(res) 之后发布merge