I am trying to get rolling means and sums over a set of column names and periods. I need to generalise the function as the number of columns and periods lead to a cartesian product of >1000. So far i have managed to make period a parameter, but am unable to do so for the column.
Sample code that i want:
periods = c(1, 3, 5)
col_names = c(SALES, DROPOUT)
for(period in periods) {
for(col_name in col_names) {
df[, c(
paste0('MEAN_', col_name, '_', period),
paste0('SUM_', col_name, '_', period)
) := list(
rollapply(col_name, width=period, fill=NA, partial=T, align='right', FUN=function(x) mean(x, na.rm=T)),
rollapply(col_name, width=period, fill=NA, partial=T, align='right', FUN=function(x) sum(x, na.rm=T))
)]
}
}
However the above is rendering a error:
Error in sum(x, na.rm = T) : invalid 'type' (character) of argument
It does not recognise col_name as a column name on which to run the mean function on. Any hints as to how to make the column name a parameter?