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:
( 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 uses for evaluation. 用于评估的规则。
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]])))
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.