testdata=data.table(c(1,1, 1, 2, 2), c(1,2,3, 4, 5), c( "a", "a", "b", "a", "b"))
colnames(testdata)=c("ID", "VAL", "CLASS")
#test dcast.data.table within function with internally declared aggregate fun -> FAILS
test_dcast_dt2 <- function(data) {
testfunc2 <- function(x) {
sum(x)
}
data_cast=dcast.data.table(data, "ID ~ CLASS", value.var="VAL", fun.aggregate=testfunc2)
}
res2=test_dcast_dt2(testdata)
Referring to issue #713 I think I just found a related bug.
Declaring and passing an aggregation function within a function calling dcast.data.table fails on my machine
R version 3.2.2 (2015-08-14)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.2 (Yosemite)
data.table_1.9.6
Here is an example: