【问题标题】:r data.table Join In Place Multiple Columnsr data.table 就地加入多列
【发布时间】:2018-10-30 18:52:44
【问题描述】:

data.table 太棒了。

我想进行就地连接,但要保留两个表中的所有列。 This question 演示了如何为单个列执行此操作。当我希望连接表中的所有列都出现在最终结果中并在一个内存位置完成时,我该如何概括这一点。

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

setkey(dt1, col1)

set.seed(1)
dt2 <- data.table(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))

setkey(dt2, col1)

# I want to stick the columns from dt1 onto dt2

# this works
dt3 <- dt2[dt1]
dt3
    col1 another_col and_anouther col2  col3
 1:    a           9        FALSE    1  TRUE
 2:    b           2        FALSE    2 FALSE
 3:    b           9        FALSE    2 FALSE
 4:    b           6        FALSE    2 FALSE
 5:    b           5         TRUE    2 FALSE
 6:    b           8        FALSE    2 FALSE
 7:    c           9         TRUE    3 FALSE
 8:    c           5        FALSE    3 FALSE
 9:    c           7        FALSE    3 FALSE
10:    c           6        FALSE    3 FALSE

# but i want to do this by reference

# this works for one column
dt2[dt1, col2 := i.col2]
dt2

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

# ok, remove that column
dt2[, col2 := NULL]

# now try to join multiple columns 
# this doesn't work
dt2[dt1, (col2 := i.col2, 
          col3 := i.col3)]

# neither does this
dt2[dt1, .(col2 := i.col2, 
          col3 := i.col3)]

# this just give me to the two columns
dt2[dt1, .(col2 = i.col2, 
           col3 = i.col3)]
dt2
   col2  col3
 1:    1  TRUE
 2:    1  TRUE
 3:    1  TRUE
 4:    2 FALSE
 5:    2 FALSE
 6:    2 FALSE
 7:    2 FALSE
 8:    3 FALSE
 9:    3 FALSE
10:    3 FALSE  

                ^

reprex package (v0.2.1) 于 2018 年 10 月 30 日创建

差不多,我想要来自dt3 的结果,但我希望它通过引用作为dt2 就地创建。谢谢!

【问题讨论】:

    标签: r data.table


    【解决方案1】:

    我应该查看链接到此awesome reference.one more questions。我需要做的就是使用:= 运算符的函数形式。

    dt2[dt1, `:=` (col2 = i.col2, 
              col3 = i.col3)]
    
    dt2
        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
    

    【讨论】:

    • 小插图确实不错,但帮助文本?":=" 也是如此,其中描述了LHS := RHS 形式和功能形式。
    • 有没有一种无需输入所有列名的方法?
    • this question中解释了一种无需输入即可合并所有列的方法
    【解决方案2】:

    函数式语法比标准方式更简洁。

    dt2[dt1, c("col2", "col3") := .(col2, col3), on = c(col1 = "col1")][order(col1)]
    
        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
    

    【讨论】:

      猜你喜欢
      • 2022-06-28
      • 1970-01-01
      • 2017-06-22
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      • 2021-09-21
      相关资源
      最近更新 更多