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

后端 未结 3 1779
北海茫月
北海茫月 2021-01-20 22:42

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 programmat

相关标签:
3条回答
  • 2021-01-20 23:01

    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
    
    0 讨论(0)
  • 2021-01-20 23:10

    You could consider changing the column names inside your function:

    groupMeans <- function(out.var, by.var, dat = dt) {
      res <- dat[, list(mean(x)), by=by.var]
      setnames(res, "V1", out.var)
      res
    }
    
    0 讨论(0)
  • 2021-01-20 23:23

    We could use setnames to name the summarised column with the 'out.var' vector.

    groupMeans <- function(out.var, by.var, dat = dt) {
      setnames(dat[, list(mean(x)), by = by.var], 
                     length(by.var)+1L, out.var)
    }
    
    groupMeans("group.var","by.var", dt)[]
    #    by.var  group.var
    #1:      a -0.1159832
    #2:      b  0.2910531
    

    EDIT: Based on @Frank's suggestion.

    0 讨论(0)
提交回复
热议问题