Home > Enterprise >  How to use frollapply to a data.table in R
How to use frollapply to a data.table in R

Time:08-18

I have to use frollapply for a function having sliding window size 26. if panel is a data.table how can I use it for following function

testFn <- function(panel){
panel <- panel[, .(variable.weeks = max(QUANTITY_2)), 
                          by = c("ITEM", "Date", "QUANTITY", "MIN_PRICE", "saleflag")]

 return(panel)

}

when I am using

library(data.table)
setDT(panel.merged)
panel<- frollapply(panel.merged,26, testFn) 

which gives error

Error in .(variable.weeks = max(QUANTITY_2)) : 
could not find function "."

CodePudding user response:

frollapply passes a vector to its function, not the whole frame. You can see this by debugging the function (debug(testFn)), calling frollapply(panel.merged,26, testFn), and confirming that panel inside the function is just a vector.

My guess is that you need something like

panel.merged[, variable.weeks := frollapply(QUANTITY_2, 26, max),
             by = c("ITEM", "Date", "QUANTITY", "MIN_PRICE", "saleflag")]
  • Related