我一直得到这个错误:
Error in eval(expr, envir, enclos) :
could not find function "aggregation.method"
示例:
#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)
我尝试过引号组合,noquote,
fun.aggregate = get(aggregation.method)
一切都无济于事。请保存我的理智!
答案 0 :(得分:1)
我不确定这是预期的行为还是错误,但以下似乎有效:
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
)