【问题标题】:Changing the column name of the column, by which is merged, to the name from the specified dataset将合并的列的列名更改为指定数据集中的名称
【发布时间】:2020-09-11 00:00:51
【问题描述】:

如果我有以下data.tables:

编辑:将cin dt1 更改为d,以防止dt1[dt2, on=.(col1= adapted_col1 )] 成为问题的解决方案。

library(data.table)
dt1 <- data.table(col1 = c("a", "b", "d"), 
                  col2 = 1:3, 
                  col3 = c(TRUE, FALSE, FALSE))

set.seed(1)
dt2 <- data.table(adapted_col1 = sample(c("a", "b", "c"), size = 10, replace = TRUE), 
                  another_col = sample(1:10, size = 10, replace = TRUE), 
                  and_anouther = sample(c(TRUE, FALSE), size = 10, replace = TRUE))

我将它们合并如下:

dt2[dt1, on=.(adapted_col1 = col1)]

    adapted_col1 another_col and_anouther col2  col3
 1:            a           3        FALSE    1  TRUE
 2:            a           8         TRUE    1  TRUE
 3:            a           8         TRUE    1  TRUE
 4:            b           2         TRUE    2 FALSE
 5:            b           7        FALSE    2 FALSE
 6:            b          10         TRUE    2 FALSE
 7:            b           4        FALSE    2 FALSE
 8:            c           4         TRUE    3 FALSE
 9:            c           5         TRUE    3 FALSE
10:            c           8         TRUE    3 FALSE

该列获取dt2 中列名的名称。但是,由于我将与dt1 重复合并,因此我需要新的data.table 具有来自dt1 的列名。我可以更改吗?

我找到的唯一解决方案是 this post,它使用不同的包,但我宁愿只使用 data.table。

【问题讨论】:

  • 好吧,愚蠢的问题,但在这种情况下你不能只切换 dt1 和 dt2 吗?
  • 您好,感谢您的评论。好吧,它并没有真正产生相同的数据哈哈..
  • 它们不是完全等价的吗? dt1[dt2, on=.( col1 = adapted_col1)][order(col1, another_col, and_anouther), .(col1, col2, col3, another_col, and_anouther)] dt2[dt1, on=.( adapted_col1 = col1)][order(adapted_col1, another_col, and_anouther), .(col1 = adapted_col1, col2, col3, another_col, and_anouther)]
  • 在这个具体的例子中是的,但它并不适用于我的实际数据。如果我想要这个特定示例的解决方案,我显然可以手动更改名称。关键是要找到一个通用的解决方案。但也许我应该创造一个比我猜想的更好的例子(见编辑)。
  • 这个答案对你有帮助吗?如果是这样,请将其标记为已接受,以表示社区知道它。否则,请指出缺少的内容。谢谢!

标签: r merge data.table


【解决方案1】:

澄清后,我建议为此编写自己的函数:

correct_name_join <- function(dt1, dt2, ...){
  dots <- vapply(substitute(...()), deparse, NA_character_)
  dt <- dt1[dt2, on = dots]
  setnames(dt, old = names(dots), new = as.character(dots))
  return(dt)
}

correct_name_join(dt2, dt1, adapted_col1 = col1)
   col1 another_col and_anouther col2  col3
1:    a           3         TRUE    1  TRUE
2:    a           5         TRUE    1  TRUE
3:    a          10         TRUE    1  TRUE
4:    b           5        FALSE    2 FALSE
5:    b           7        FALSE    2 FALSE
6:    b           9        FALSE    2 FALSE
7:    d          NA           NA    3 FALSE

dt2[dt1, on=.(adapted_col1 = col1)]
   adapted_col1 another_col and_anouther col2  col3
1:            a           3         TRUE    1  TRUE
2:            a           5         TRUE    1  TRUE
3:            a          10         TRUE    1  TRUE
4:            b           5        FALSE    2 FALSE
5:            b           7        FALSE    2 FALSE
6:            b           9        FALSE    2 FALSE
7:            d          NA           NA    3 FALSE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 2021-03-29
    • 2019-08-11
    相关资源
    最近更新 更多