5

I want to put data.table dcast function into a function, which can handle custom number/order of aggregate functions. That is why I need to pass aggregate functions as parameters to dcast function. The parameters need to be defined outside dcast. How could I do this?

This works well, but I wan to define the aggregate functions outside of dcast.

dt = data.table(x = sample(5, 20, TRUE), y = sample(2, 20, TRUE), 
                z = sample(letters[1:2], 20, TRUE), d1 = runif(20), d2 = 1L

dcast(dt, x + y ~ z, fun = list(sum, min), value.var = "d1")

I tried this method:

func <- list(sum, min)

dcast(dt, x + y ~ z, fun = func, value.var = "d1")

Then I get this error message:

Error in eval(expr, envir, enclos) : could not find function "func"

Henrik
  • 65,555
  • 14
  • 143
  • 159
tgyozo
  • 51
  • 2

0 Answers0