Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass names for new summary columns to data.table in a function?

Tags:

r

data.table

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?

like image 939
Jake Fisher Avatar asked Nov 15 '25 18:11

Jake Fisher


1 Answers

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
like image 82
Jake Fisher Avatar answered Nov 17 '25 10:11

Jake Fisher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!