Say I want to create a function that calculates a summary dataset from a data.table in R, and I want to be able to pass the name of the new calculated variable in programmatically.
For example:
library(data.table)
# generate some fake data
set.seed(919)
dt <- data.table(x = rnorm(50), by.var = rep(c("a", "b"), 25))
dt[, list(group.means = mean(x)), by = "by.var"] # This is what I want
# But I want to do in a function, so I can do it repeatedly:
groupMeans <- function(out.var, by.var, dat = dt) {
return(dat[, list(out.var = mean(x)), by = by.var]) # doesn't work
}
groupMeans("group.means", "by.var") # out.var should be "group.means"
How do I do this?
Courtesy of docendo discimus, you can use a named list created with setNames, like this:
groupMeans <- function(out.var, by.var, dat = dt) {
return(dat[, setNames(list(mean(x)), out.var), by = by.var])
}
groupMeans("group.means", "by.var")
# by.var group.means
# 1: a -0.1159832
# 2: b 0.2910531
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With