简体   繁体   English

将data.table列分配给R中的变量

[英]Assign a data.table column to a variable in R

I have a problem about the assignment of data.table columns in R. My example code is like below: 我在R中分配data.table列有一个问题。我的示例代码如下所示:

library(data.table)
DT <- data.table(A=c(3,5,2,6,4), B=c(6,2,7,2,1), Amount=1:5)
setkey(DT, A)
amt <- DT$Amount 
amt #3 1 5 2 4
setkey(DT, B)
amt #5 2 4 1 3

I used the "$" sign to assign the data.table's column to a variable "amt", but looks like after I changed the order of the data.table, the order of "amt" is changed as well. 我使用“$”符号将data.table的列分配给变量“amt”,但看起来在我更改了data.table的顺序之后,“amt”的顺序也被更改了。 Can anyone tell me why this happens? 谁能告诉我为什么会这样? and how can I avoid this from happening (I dont want the order of "amt" to change when I change the order of DT)? 我怎样才能避免这种情况的发生(当我改变DT的顺序时,我不希望改变“amt”的顺序)?

Thank you very much. 非常感谢你。

To get around this, you can take a copy of the column: 要解决此问题,您可以获取该列的副本:

amt <- copy(DT$Amount)

When assigning amt <- DT$Amount , the result is a "shallow copy," which is simply a pointer to the original column. 当分配amt <- DT$Amount ,结果是“浅拷贝”,它只是指向原始列的指针。 The same issue comes up when you want to create a copy of a data.table , where best practice is DT2 <- copy(DT) . 当您想要创建data.table的副本时,会出现同样的问题,其中最佳做法是DT2 <- copy(DT)

Note that data.tables -- like data.frames, of which they are a special case -- are each a vector of pointers to columns; 请注意,data.tables - 就像data.frames一样,它们是一个特例 - 每个都是指向列的指针的向量; and that this copying behavior is inherited from base R. For example: 并且此复制行为是从基础R继承的。例如:

DF <- data.frame(x=c(1,4,2)); xx <- DF$x; setorder(DF,x); identical(xx,DF$x) # TRUE

The link above is strongly recommended for both technical details and advice on best practices. 强烈建议使用上面的链接来获取技术细节和最佳实践建议。

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

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