何时应在 data.table 中使用 := 运算符? [英] When should I use the := operator in data.table?

查看:12
本文介绍了何时应在 data.table 中使用 := 运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

data.table 对象现在有一个 := 运算符.是什么让这个运算符与所有其他赋值运算符不同?另外,它的用途是什么,它的速度有多快,什么时候应该避免?

data.table objects now have a := operator. What makes this operator different from all other assignment operators? Also, what are its uses, how much faster is it, and when should it be avoided?

推荐答案

这是一个例子,显示 10 分钟减少到 1 秒(来自 主页).这就像对 data.frame 进行子分配,但不会每次都复制整个表.

Here is an example showing 10 minutes reduced to 1 second (from NEWS on homepage). It's like subassigning to a data.frame but doesn't copy the entire table each time.

m = matrix(1,nrow=100000,ncol=100)
DF = as.data.frame(m)
DT = as.data.table(m)

system.time(for (i in 1:1000) DF[i,1] <- i)
     user  system elapsed 
  287.062 302.627 591.984 

system.time(for (i in 1:1000) DT[i,V1:=i])
     user  system elapsed 
    1.148   0.000   1.158     ( 511 times faster )

像这样将 := 放在 j 中可以使用更多成语:

Putting the := in j like that allows more idioms :

DT["a",done:=TRUE]   # binary search for group 'a' and set a flag
DT[,newcol:=42]      # add a new column by reference (no copy of existing data)
DT[,col:=NULL]       # remove a column by reference

和:

DT[,newcol:=sum(v),by=group]  # like a fast transform() by group

我想不出任何避免 := 的理由!除此之外,在 for 循环内.由于 := 出现在 DT[...] 内部,它带来了 [.data.table 方法的少量开销;例如,S3 调度并检查参数的存在和类型,例如 ibynomatch 等.所以对于内部 for 循环,有一个低开销,直接版本的 := 称为 set.请参阅 ?set 了解更多详细信息和示例.set 的缺点包括 i 必须是行号(不能进行二分查找),并且不能与 by 结合使用.通过设置这些限制 set 可以显着减少开销.

I can't think of any reasons to avoid := ! Other than, inside a for loop. Since := appears inside DT[...], it comes with the small overhead of the [.data.table method; e.g., S3 dispatch and checking for the presence and type of arguments such as i, by, nomatch etc. So for inside for loops, there is a low overhead, direct version of := called set. See ?set for more details and examples. The disadvantages of set include that i must be row numbers (no binary search) and you can't combine it with by. By making those restrictions set can reduce the overhead dramatically.

system.time(for (i in 1:1000) set(DT,i,"V1",i))
     user  system elapsed 
    0.016   0.000   0.018

这篇关于何时应在 data.table 中使用 := 运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆