I am using data.table 1.10.0
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.data.table(dt, x + y ~ z, fun.aggregate=list(sum, mean), value.var=list("d1", "d2"))
It can work well.
x y d1_sum_a d1_sum_b d2_mean_a d2_mean_b
1: 1 1 0.57820795 0.000000 1 NaN
2: 1 2 0.57323882 1.030819 1 1
3: 2 2 0.50448265 2.236394 1 1
4: 3 1 0.04189855 1.010506 1 1
5: 3 2 0.94923490 0.000000 1 NaN
6: 4 1 0.44673494 1.112272 1 1
7: 5 1 0.91163728 0.000000 1 NaN
8: 5 2 0.60146531 0.000000 1 NaN
I am trying to do like this:
fun.list <- list(sum, mean)
value.list <- list("d1", "d2")
dcast.data.table(dt, x + y ~ z, fun.aggregate=fun.list, value.var=value.list)
Error occurs:
Error in aggregate_funs(fun.call, lvals, sep, ...) :
When 'fun.aggregate' and 'value.var' are both lists, 'value.var' must be either of length =1 or =length(fun.aggregate).
But length(fun.list) is eque to length(value.list).
length(fun.list)
[1] 2
length(value.list)
[1] 2
I am using data.table 1.10.0
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.data.table(dt, x + y ~ z, fun.aggregate=list(sum, mean), value.var=list("d1", "d2"))It can work well.
I am trying to do like this:
fun.list <- list(sum, mean)value.list <- list("d1", "d2")dcast.data.table(dt, x + y ~ z, fun.aggregate=fun.list, value.var=value.list)Error occurs:
But length(fun.list) is eque to length(value.list).
length(fun.list)length(value.list)