Reputation: 87
I consistantly get this error:
Error in eval(expr, envir, enclos) :
could not find function "aggregation.method"
Example:
#sample data
Date.Time<- seq(ISOdate(2000,1,1), by = "min" , length.out = 200)
rh <- as.data.table(cbind(Date.Time,rnorm(200),rnorm(200),rnorm(200)))
colnames(rh) <- c("Date.Time", "A", "B", "C")
rh$Date.time <- as.POSIXct( rh$Date.Time, origin = "1970-01-01" )
#Custom function
Custom.data.wrangle<-function(x, timeinterval, aggregation.method)
{
require(data.table)
x$Date.Time <- as.POSIXct(as.integer(as.numeric(x$Date.Time) / (60 * timeinterval)) * (60 * timeinterval),
origin = "1970-01-01" ,
tz = "GMT")# Rounddown POSIXct in a simple form.
x<-melt(x, id= "Date.Time")
x<-dcast(x, Date.Time~variable, fun.aggregate = aggregation.method, na.rm=TRUE) #Restructure the table
return(x)
}
rh<- Custom.data.wrangle(rh, 30, mean)
I've tried a combination of quotes, noquote,
fun.aggregate = get(aggregation.method)
all to no avail. Please save my sanity!
Upvotes: 1
Views: 970
Reputation: 18602
I'm not sure if this is intended behavior or a bug, but the following seems to work:
f <- function(agg.fun) {
dcast(
DT,
time ~ variable,
fun.aggregate = eval(substitute(agg.fun)),
# ^^^^^^^^^^^^^^^^^^^^^^^^^
na.rm = TRUE
)
}
f(mean)
# time weight
# 1: 0 41.06000
# 2: 2 49.22000
# 3: 4 59.95918
# 4: 6 74.30612
# 5: 8 91.24490
# 6: 10 107.83673
# 7: 12 129.24490
# 8: 14 143.81250
# 9: 16 168.08511
# 10: 18 190.19149
# 11: 20 209.71739
# 12: 21 218.68889
library(data.table)
DT <- melt(
setnames(
as.data.table(ChickWeight),
names(ChickWeight), tolower(names(ChickWeight))
),
id = 2:4
)
Upvotes: 1