data.table不再通过引用更新 [英] data.table not updating by reference anymore
问题描述
这里是一个函数
f <- function(orderData){
colNames <- paste0("lim_",sort(unique(orderData[,XLM])))
orderData[, (colNames):={lim_=factor(XLM);lapply(data.table(model.matrix(~ lim_:w_qalim + 0)), cumsum)}]
}
>
and some sample data
dt = data.table(XLM=sample(1L:300L,5e4,T), w_qalim=sample(1L:5L,5e4,T))
dt1 = data.table(XLM=sample(1L:300L,1e2,T), w_qalim=sample(1L:5L,1e2,T))
执行 f(dt)
不更新 dt
通过引用在我的框,但 f(dt1)
做。是否为 datatable.alloccol
Executing f(dt)
does not update dt
by reference on my box but f(dt1)
do. Is it expected/ something to do with datatable.alloccol
推荐答案
两种情况下,数据表的长度为2并且长度为100:
In both cases the length of the data table is 2 and the truelength is 100:
> length(dt); truelength(dt)
[1] 2
[1] 100
> length(dt1); truelength(dt1)
[1] 2
[1] 100
,在 dt
colNames
的情况下为300,所以2 + 300超过了长度,但是在 dt1
colNames
是81所以2 + 81不。
however, in the case of dt
colNames
is 300 so 2+300 exceeds the truelength but in the case of dt1
colNames
is 81 so 2+81 does not.
可以预先分配更大的长度,例如
You can either allocate a larger truelength in advance, e.g.
alloc.col(dt, 1000)
或者您可以设置默认值,以便所有数据表都有较大的默认值:
or you can set the default so that all data tables have a larger default:
options(datatable.alloccol = 1000)
请参阅?alloc.col
,
这篇关于data.table不再通过引用更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!