我尝试使用ddply
.
首先,一些示例数据:
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)
)
head(dfx)
# group sex age
# 1 A F 53.08787
# 2 A M 30.47225
# 3 A F 26.78341
# 4 A F 26.46841
# 5 A F 34.65360
# 6 A M 21.26691
这是您可能会尝试的方法(我假设您打算summarize
在您的问题中使用)。
library(plyr)
ddply(dfx, .(group, sex), summarize, varA = sum(age > 25))
# group sex varA
# 1 A F 5
# 2 A M 1
# 3 B F 6
# 4 B M 4
# 5 C F 3
# 6 C M 2
然后我们可能会尝试在函数中使用它,如下所示:
func <- function(val.in) {
out <- ddply(dfx, .(group, sex), summarize, varA = sum(age > val.in))
out
}
func(25)
# Error in eval(expr, envir, enclos) : object 'val.in' not found
^^这是你的错误^^
最直接的解决方案是使用here
(这有助于ddply
找出在哪里寻找东西):
func <- function(val.in) {
out <- ddply(dfx, .(group, sex), here(summarize), varA = sum(age > val.in))
out
}
func(25)
# group sex varA
# 1 A F 5
# 2 A M 1
# 3 B F 6
# 4 B M 4
# 5 C F 3
# 6 C M 2
更新
据我所知,这在“dplyr”中似乎不是问题:
library(dplyr)
myFun <- function(val.in) {
dfx %>% group_by(group, sex) %>% summarise(varA = sum(age > val.in))
}
myFun(10)
# Source: local data frame [6 x 3]
# Groups: group
#
# group sex varA
# 1 A F 5
# 2 A M 3
# 3 B F 7
# 4 B M 8
# 5 C F 2
# 6 C M 4