问题描述
我一直在努力解决这个问题,这与。不知怎的,我无法将该问题中提供的解决方案翻译成我自己的问题。
I've been struggling with this issue which is quite similar to a question raised here before. Somehow I can't translate the solution given in that question to my own problem.
我首先提供一个示例数据框:
I start off with making an example data frame:
test.df <- data.frame(col1 = rep(c('a','b'), each=5), col2 = runif(10))
str(test.df)
以下函数应创建一个新数据框架与statvar的平均值基于groupvar的组。
The following function should create a new data frame with the mean of a "statvar" based on groups of a "groupvar".
test.f <- function(df, groupvar, statvar) {
df %>%
group_by_(groupvar) %>%
select_(statvar) %>%
summarise_(
avg = ~mean(statvar, na.rm = TRUE)
)
}
test.f(df = test.df,
groupvar = "col1",
statvar = "col2")
我想要返回的是一个数据帧为2个计算平均值(一个用于col1中的一个值,一个用于col1中的所有b值)。取而代之的是:
What I would like this to return is a data frame with 2 calculated averages (one for all a values in col1 and one for all b values in col1). Instead I get this:
col1 avg
1 a NA
2 b NA
Warning messages:
1: In mean.default("col2", na.rm = TRUE) :
argument is not numeric or logical: returning NA
2: In mean.default("col2", na.rm = TRUE) :
argument is not numeric or logical: returning NA
我发现这个奇怪的原因我很确定col2是数字:
I find this strange cause I'm pretty sure col2 is numeric:
str(test.df)
'data.frame': 10 obs. of 2 variables:
$ col1: Factor w/ 2 levels "a","b": 1 1 1 1 1 2 2 2 2 2
$ col2: num 0.4269 0.1928 0.7766 0.0865 0.1798 ...
推荐答案
library(lazyeval)
library(dplyr)
test.f <- function(df, groupvar, statvar) {
test.df %>%
group_by_(groupvar) %>%
select_(statvar) %>%
summarise_(
avg = (~mean(statvar, na.rm = TRUE)) %>%
interp(statvar = as.name(statvar))
)
}
test.f(df = test.df,
groupvar = "col1",
statvar = "col2")
您的问题是col2 statvar, mean(col2)
未定义
Your issue is that "col2" is being substituted for statvar, and the mean("col2")
is undefined
这篇关于在另一个函数中使用dplyr函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!