Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use dot dot dot (...) to indicate columns returned from a self-defined function for a data.table object

I want to use ... to indicate the variables I want to return from a self-defined function for a data.table object. Here is a minimal replicable example:

library(data.table)
d = data.table(mtcars)

getvar = function(...){
  return(d[,.(xyz = mean(hp), ...), cyl])
}

getvar(mpg, cyl, disp)

Error in [.data.table(d, , .(N = .N, ...), cyl) : object 'cyl' not found

What I wish to get is:

d[,.(xyz = mean(hp), mpg, cyl, disp), cyl]

 #    cyl       xyz  mpg cyl  disp
 # 1:   6 122.28571 21.0   6 160.0
 # 2:   6 122.28571 21.0   6 160.0
 # 3:   6 122.28571 21.4   6 258.0
 # 4:   6 122.28571 18.1   6 225.0
 # 5:   6 122.28571 19.2   6 167.6

Anyone can share their solutions?

like image 249
Miao Cai Avatar asked Dec 22 '22 20:12

Miao Cai


2 Answers

A possible solution is using mget in your function wich returns a list and then combining xyz with that with c. The columns that you want to add need to be specified as a character vector to make this work:

getvar = function(...){
  return(d[, c(xyz = mean(hp), mget(...)), cyl])
}

getvar(c("mpg", "cyl", "disp"))

which gives:

> getvar(c("mpg", "cyl", "disp"))
    cyl       xyz  mpg cyl  disp
 1:   6 122.28571 21.0   6 160.0
 2:   6 122.28571 21.0   6 160.0
 3:   6 122.28571 21.4   6 258.0
 4:   6 122.28571 18.1   6 225.0
 5:   6 122.28571 19.2   6 167.6
 6:   6 122.28571 17.8   6 167.6
 7:   6 122.28571 19.7   6 145.0
 8:   4  82.63636 22.8   4 108.0
 9:   4  82.63636 24.4   4 146.7
10:   4  82.63636 22.8   4 140.8
....

Or as an alternative a slight variation of @Rhonak's answer (thx to @zx8754):

getvar = function(...){
  mc <- match.call(expand.dots = FALSE)
  x <- as.character(mc$...)
  d[, c(xyz = mean(hp), mget(x)), cyl]
}

getvar(mpg, cyl, disp)
like image 156
Jaap Avatar answered Dec 25 '22 09:12

Jaap


To get this to work without quoting the column names, you'd have to use some non-standard evaluation tactics:

getvar = function(...){
  vars <- substitute(list(xyz = mean(hp), ...))
  return(d[, eval(vars), cyl])
}

getvar(mpg, cyl, disp)
    cyl       xyz  mpg cyl  disp
 1:   6 122.28571 21.0   6 160.0
 2:   6 122.28571 21.0   6 160.0
 3:   6 122.28571 21.4   6 258.0
 4:   6 122.28571 18.1   6 225.0
 5:   6 122.28571 19.2   6 167.6
...etc...
like image 38
teunbrand Avatar answered Dec 25 '22 09:12

teunbrand