passing parameters to R function used plyr
r
Solution
In this case it might be easier to pass a function to `ddply` instead of using `summarize`:
f.describe <- function(.data, .var, .by) {
require(plyr)
ddply(.data, .by, function(d) {
c(N = sum(!is.na(d[[.var]])),
`Mean (SD)`=sprintf("%5.2f (%5.2f)",
mean(d[[.var]], na.rm=TRUE),
sd=sd(d[[.var]], na.rm=TRUE)),
Median = sprintf("%5.2f", median(d[[.var]])))
})
}
Problem
I can'n solve one question. Want to write a function like this: ``` f.describe <- function(.data, .var, .by) { require(plyr) res <- ddply(.data, .by, summarize, N = sum(!is.na(.var)) `Mean (SD)`=sprintf("%5.2f (%5.2f)", mean(.var, na.rm=TRUE), sd=sd(.var, na.rm=TRUE)), Median = sprintf("%5.2f", median(.var)) ) res } ``` But I can't find the way to pass a variable for processing (.var). Have this error: Error eval(expr, envir, enclos) : object '.var' was not found (translated from other language, so it could be not verbatim for English users) I tried eval, substitute but have no solution... Many thanks for help. Sometimes I do not understand the rules R uses for evaluation.