我已经查看了几个地方,但是我就是想不出如何做到这一点。它似乎已经改变了几次,所以更加令人困惑。
我想作为一个函数来总结Endoscopist的NumOfBx。我有以下数据帧:
vv <- structure(list(Endoscopist = c("John Boy ", "Jupi Ter ", "Jupi Ter ",
"John Boy ", "John Boy ", "John Boy ", "Mar Gret ", "John Boy ",
"Mar Gret ", "Phil Ip ", "Phil Ip "), NumbOfBx = c(2, 4, NA,
2, 12, 12, NA, NA, NA, 3, NA)), row.names = 100:110, .Names = c("Endoscopist",
"NumbOfBx"), class = "data.frame")
我的函数是:
NumBx <- function(x, y, z) {
x <- data.frame(x)
x <- x[!is.na(x[,y]), ]
NumBxPlot <- x %>% group_by_(z) %>% summarise(avg = mean(y, na.rm = T))
}
我用以下方式调用它:
NumBx(vv,"Endoscopist","NumOfBx)
这个给了我一个错误:
Warning messages:
1: In mean.default(y, na.rm = T) :
argument is not numeric or logical: returning NA
2: In mean.default(y, na.rm = T) :
argument is not numeric or logical: returning NA
3: In mean.default(y, na.rm = T) :
argument is not numeric or logical: returning NA
我将函数更改为使用summarise_
,但结果仍然相同。后来我意识到需要特别使用summarise_
(而不是group_by_
),需要标准评估,于是我尝试了这个方法(来自这个stackoverflow示例)
library(lazyeval)
NumBx <- function(x, y, z) {
x <- data.frame(x)
x <- x[!is.na(x[,y]), ]
NumBxPlot <- x %>% group_by_(z) %>%
summarise_(sum_val = interp(~mean(y, na.rm = TRUE), var = as.name(y)))
但我仍然会得到相同的错误信息:
Warning messages:
1: In mean.default(y, na.rm = T) :
argument is not numeric or logical: returning NA
2: In mean.default(y, na.rm = T) :
argument is not numeric or logical: returning NA
3: In mean.default(y, na.rm = T) :
argument is not numeric or logical: returning NA
我想要的输出是:
Endoscopist Avg
Jupi Ter 4
John Boy 28
Phil Ip 3
get(y)
。我测试过了,当尝试使用一个变量来引用列名时,我得到了相同的错误。get()
函数解决了这个问题。你的group_by
可能也需要做同样的处理。 - Balter