简体   繁体   English

在功能中将列合并到data.table

[英]Merge column to data.table in function

I need to add a column to a huge data.table dt.1 (from a lookup table dt.2). 我需要在巨大的data.table dt.1中添加一个列(来自查找表dt.2)。 It can be done like this: 它可以这样做:

df.1 <- cbind(c(1,2,3,1,2,3,1,2,3),c(1,2,3,1,2,3,1,2,3),c(1,2,3,4,5,6,7,8,9))
colnames(df.1) <- c("ColA", "ColB", "ColC")
df.2 <- cbind(c(1,3),c(1,3),c(10,11))
colnames(df.2) <- c("ColA", "ColB", "ColD")
dt.1 <- data.table(df.1)
dt.2 <- data.table(df.2)

getAnotherColumn <- function() {
  keycols <- c("ColA", "ColB")
  setkeyv(dt.1, keycols)
  setkeyv(dt.2, keycols)
  dt.1 <- merge(dt.1, dt.2, all=TRUE)
  dt.1  # Will print with ColA, ColB, ColC, and ColD. As needed.
}

getAnotherColumn()
dt.1  # Only ColA, ColB, and ColC are here. ColD is also needed.

The problem is that I must do it inside a function, so at the return from the function, the old dt.1 is still there (without the new column ColD). 问题是我必须在函数内部执行它,所以在从函数返回时,旧的dt.1仍然存在(没有新的列ColD)。

How can I add column to dt.1 instead of creating a new dt.1? 如何将列添加到dt.1而不是创建新的dt.1?

As dt.1 is huge, I cannot afford to make "extra copies" of it. 由于dt.1是巨大的,我无法承担它的“额外副本”。

Notice that inside the function, you are changing the object dt.1 during this line: 请注意,在函数内部,您将在此行中更改对象dt.1

   dt.1 <- merge(dt.1, dt.2, all=TRUE)

You are assigning a completely new value to the object dt.1 . 您正在为对象dt.1分配一个全新的值。 It just so happens that the new value is related to the previous value of dt.1 , but this is no different than, say, dt.1 ,新值与dt.1的先前值相关,但这与dt.1 ,没有什么不同

   dt.1 <- "spaghetti"  # or any other unrelated value

To assign by reference, you need to use := 要通过引用分配,您需要使用:=

getAnotherColumn <- function() {
  keycols <- c("ColA", "ColB")
  setkeyv(dt.1, keycols)
  setkeyv(dt.2, keycols)

  dt.1[dt.2, ColD := ColD]
}

getAnotherColumn()

Now ColD is in dt.1 现在ColDdt.1

> dt.1 
   ColA ColB ColC ColD
1:    1    1    1   10
2:    1    1    4   10
3:    1    1    7   10
4:    2    2    2   NA
5:    2    2    5   NA
6:    2    2    8   NA
7:    3    3    3   11
8:    3    3    6   11
9:    3    3    9   11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM