【问题标题】:Merging 2, 1 row data.tables on column names合并列名上的 2、1 行 data.tables
【发布时间】:2021-04-20 16:24:23
【问题描述】:

我有 2、1 行 data.tables。

dt1 <- data.table(a = 0, b = 0, c = 0)
dt2 <- data.table(a = 1, c = 5)

我的目标是用dt2 的值更新dt1,以便我得到:

desired_dt <- data.table(a = 1, b = 0, c = 5) 

我尝试合并,假设它与共享列名匹配,但没有运气。

dt2[dt1]
Error in `[.data.table`(dt2, dt1) : 
When i is a data.table (or character vector), the columns to join by must be specified using 'on=' 
argument (see ?data.table), by keying x (i.e. sorted, and, marked as sorted, see ?setkey), or by 
sharing column names between x and i (i.e., a natural join). Keyed joins might have further speed 
benefits on very large data due to x being sorted in RAM.

这不会被视为自然连接吗?对于我所缺少的内容,我将不胜感激!

【问题讨论】:

    标签: r merge data.table


    【解决方案1】:

    我们得到intersecting 列名并进行赋值

    nm1 <- intersect(names(dt1), names(dt2))
    dt1[, (nm1) := dt2]
    

    或者我们可以set密钥

    setkeyv(dt1, intersect(names(dt1), names(dt2)))
    out <- dt1[dt2]
    for(j in seq_along(out)) set(out, i = which(is.na(out[[j]])), j=j, value = 0)
    

    【讨论】:

    • 谢谢!第一个解决方案有效,我不会考虑。然而,第二个解决方案应该保留 dt1 中的 b = 0 值,而不是用 NA 替换它
    • 你的第一个方法就像一个魅力!喜欢!
    【解决方案2】:

    这是一个data.table 选项,使用rbindlist + fcoalesce

    setcolorder(
      rbindlist(
        list(dt2, dt1),
        fill = TRUE
      )[
        ,
        lapply(.SD, function(x) fcoalesce(as.list(x)))
      ], names(dt1)
    )[]
    

    给了

       a b c
    1: 1 0 5
    

    【讨论】:

      猜你喜欢
      • 2012-12-13
      • 2015-04-24
      • 1970-01-01
      • 2021-02-03
      • 2022-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多