Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R如何根据.SD中rollapplyr的结果向data.table添加多列(:=)_R_Data.table - Fatal编程技术网

R如何根据.SD中rollapplyr的结果向data.table添加多列(:=)

R如何根据.SD中rollapplyr的结果向data.table添加多列(:=),r,data.table,R,Data.table,我正在尝试向大型data.table中添加一些列,这些列基于由唯一标识符拆分的滚动计算 基于此,我已生成此声明: alldefaults[,`:=`(.SD[,list(obsPaymentDownMax24m=rollapplyr(obsPaymentsDown,24,max,partial=TRUE) ,obsPaymentDownAvg24m=rollapplyr(obsPaymentsDown,24,mean,partial=TRUE)

我正在尝试向大型data.table中添加一些列,这些列基于由唯一标识符拆分的滚动计算

基于此,我已生成此声明:

alldefaults[,`:=`(.SD[,list(obsPaymentDownMax24m=rollapplyr(obsPaymentsDown,24,max,partial=TRUE) 
                       ,obsPaymentDownAvg24m=rollapplyr(obsPaymentsDown,24,mean,partial=TRUE)
                       ,obsPaymentDownMax12m=rollapplyr(obsPaymentsDown,12,max,partial=TRUE)
                       ,obsPaymentDownAvg12m=rollapplyr(obsPaymentsDown,12,mean,partial=TRUE))]),by=OriginalApplicationID]
它会产生一个错误

Error in `[.data.table`(alldefaults, , `:=`(.SD[, list(obsPaymentDownMax24m = rollapplyr(obsPaymentsDown,  : 
  In `:=`(col1=val1, col2=val2, ...) form, all arguments must be named.
当我在没有函数
:=
的情况下运行它时,命名为的函数工作得很好,但它是一个新的数据集,然后需要重新连接它。

在.SD中插入赋值

alldefaults[,.SD[,`:=`(list(obsPaymentDownMax24m=rollapplyr(obsPaymentsDown,24,max,partial=TRUE) 
                        ,obsPaymentDownAvg24m=rollapplyr(obsPaymentsDown,24,mean,partial=TRUE)
                        ,obsPaymentDownMax12m=rollapplyr(obsPaymentsDown,12,max,partial=TRUE)
                        ,obsPaymentDownAvg12m=rollapplyr(obsPaymentsDown,12,mean,partial=TRUE)))],by=OriginalApplicationID]
产生此错误

Error in `[.data.table`(.SD, , `:=`(list(obsPaymentDownMax24m = rollapplyr(obsPaymentsDown,  : 
  .SD is locked. Using := in .SD's j is reserved for possible future use; a tortuously flexible way to modify by group. Use := in j directly to modify by group by reference.



我错过了一个让这个更新生效的诀窍吗




PS-不确定这是否是一个足够大的问题,需要一个可复制的示例,因为它似乎主要面向语法,并且很容易指出该语句应该是什么。此外,如果有人建议再次加快速度,我将非常感激

我猜测(猜测,因为问题的形式不正确,这可能就是你被否决的原因)你想这样做:

alldefaults[,`:=`(obsPaymentDownMax24m=rollapplyr(obsPaymentsDown,24,max,partial=TRUE) 
                 ,obsPaymentDownAvg24m=rollapplyr(obsPaymentsDown,24,mean,partial=TRUE)
                 ,obsPaymentDownMax12m=rollapplyr(obsPaymentsDown,12,max,partial=TRUE)
                 ,obsPaymentDownAvg12m=rollapplyr(obsPaymentsDown,12,mean,partial=TRUE)),by=OriginalApplicationID]
-1.那就举个简单的例子吧。例如,如果您仅在一个rollappyr中看到此行为,则不需要显示四个。