I would like to give functions to be performed in the j slot of a data.table as arguments in the style of:
DT <- as.data.table(structure(list(peak.grp = c(1L, 2L, 2L, 2L, 2L), s = c(248, 264,
282, 304, 333), height = c(222772.8125, 370112.28125, 426524.03125, 649691.75, 698039)), class = "data.frame", row.names = c(NA,
-5L)))
list_of_functions_with_parameters <- list(sum = list(x = s, na.rm = TRUE), mean = list(x = height, na.rm = TRUE))
vector_of_variable_names <- c("Sum.s", "Mean.height")
vector_for_by <- c("peak.grp")
perform_dt_operations <-
function(DT, vector_of_variable_names, list_of_functions_with_parameters, vector_for_by){
DT <- DT[, .(vector_of_variable_names = list_of_functions_with_parameters), by = row.names(DT)]
return(DT)
}
The output should then be:
Output <- perform_dt_operations(DT, vector_of_variable_names, list_of_functions_with_parameters, vector_for_by)
dput(as.data.frame(Output))
structure(list(peak.grp = c(1, 2), Sum.s = c(248, 1183), Mean.height = c(222772.8125,
536091.765625)), row.names = c(NA, -2L), class = "data.frame")
Is there a way to do something like that?