0
以下幾乎最少的代碼的要點是在f定義內自定義ddply的聚合函數(幫助程序)的函數f中應用ddply。在另一個函數中定義聚合函數時出現ddply錯誤
不幸的是,我不明白爲什麼EVAL採購整個片段產生 錯誤(表達式,ENVIR,enclos):找不到功能「幫手」。當輔助函數獨立於函數f運行時,代碼工作。當我將ddply呼叫替換爲的未註釋呼叫時,代碼無誤地運行。
您能解釋錯誤並提供解決方案或解決方法嗎? [測試與plyr 1.8.1和R 3.0.3]
rm (list = ls())
library(plyr)
f <- function() {
dfx <- data.frame(
group = c(rep('A', 8), rep('B', 15), rep('C', 6)),
sex = sample(c("M", "F"), size = 29, replace = TRUE),
age = runif(n = 29, min = 18, max = 54)
)
helper <- function(x) {
return(max(x))
}
result <- ddply(dfx, .(group, sex), summarize, max_age = helper(age))
#result <- by(dfx$age, dfx[,c("group", "sex")], helper)
return(result)
}
print(f())
非常好,完美的作品!非常感謝您的快速回答。事後看來,我認爲「這裏」正是爲了這種環境問題而添加到plyr中。 – pandiani42