【问题标题】:Assign same value to all elements in the same cluster为同一簇中的所有元素分配相同的值
【发布时间】: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


    【解决方案1】:

    使用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
    

    【讨论】:

    • 谢谢!但是,我无法重现该示例 - 我总是在 Text 列中得到与初始表中相同的结果。
    • head(res) 之后发布merge
    • 非常感谢!列名不匹配,应仔细检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 2018-05-24
    • 1970-01-01
    • 2020-03-02
    • 2014-08-19
    相关资源
    最近更新 更多