考虑一下这个非常简单的例子
library(dplyr)
library(broom)
dataframe <- data_frame(id = c(1,2,3,4,5,6),
group = c(1,1,1,2,2,2),
value = c(200,400,120,300,100,100))
# A tibble: 6 x 3
id group value
<dbl> <dbl> <dbl>
1 1 1 200
2 2 1 400
3 3 1 120
4 4 2 300
5 5 2 100
6 6 2 100在这里,我想写一个函数,它输出value均值的置信估计的上界。那是,
get_ci_high <- function(data, myvar){
confint_tidy(lm(data = data, myvar ~ 1)) %>% pull(conf.high)
}现在,这很容易实现。
confint_tidy(lm(data = dataframe, value ~ 1)) %>% pull(conf.high)
[1] 332.9999这也同样有效(注意group_by之后的调用)
dataframe %>% group_by(group) %>% mutate(dealwithit = get_ci_high(., value))
# A tibble: 6 x 4
# Groups: group [2]
id group value dealwithit
<dbl> <dbl> <dbl> <dbl>
1 1 1 200 598.2674
2 2 1 400 598.2674
3 3 1 120 598.2674
4 4 2 300 453.5102
5 5 2 100 453.5102
6 6 2 100 453.5102这工作得很好。
mindblow <- function(data, groupvar, outputvar){
quo_groupvar <- enquo(groupvar)
quo_outputvar <- enquo(outputvar)
data %>% group_by(!!quo_groupvar) %>%
summarize(output = get_ci_high(., !!quo_outputvar))%>%
ungroup()
}
> mindblow(dataframe, groupvar = group, outputvar = value)
# A tibble: 2 x 2
group output
<dbl> <dbl>
1 1 598.2674
2 2 453.5102..。但是这个失败了
get_ci_high(dataframe, value)
Error in eval(expr, envir, enclos) : object 'value' not found 我不明白这里出了什么问题。我真的需要一个在上述四种情况下有效的解决方案。
有什么想法吗?非常感谢!!
发布于 2017-08-24 13:15:37
原因是当您传递value参数时,您希望R在公式中使用它的名称"value",而不是变量的值(不存在)。
一种解决方案是使用substitute() (非标准评价)提取名称,并使用as.formula创建一个公式
get_ci_high <- function(data, myvar) {
col_name <- as.character(substitute(myvar))
fmla <- as.formula(paste(col_name, "~ 1"))
confint_tidy(lm(data = data, fmla)) %>% pull(conf.high)
}
get_ci_high(dataframe, value)但是,我强烈建议将公式value ~ 1作为第二个参数传递。对于执行其他线性模型(当您有预测器时),这既简单又灵活。
get_ci_high <- function(data, fmla) {
confint_tidy(lm(data = data, fmla)) %>% pull(conf.high)
}
get_ci_high(dataframe, value ~ 1)https://stackoverflow.com/questions/45862338
复制相似问题