dplyr在函数内部不工作
我对R中的函数没有经验。我正在尝试构建一个通过目标变量计算平均值的函数(在我的示例中:Finaned_final) 我的数据:dplyr在函数内部不工作,r,function,dplyr,R,Function,Dplyr,我对R中的函数没有经验。我正在尝试构建一个通过目标变量计算平均值的函数(在我的示例中:Finaned_final) 我的数据: residential_status funded_final Living with parents 0 Rent 0 Rent 0 Own 1 Own 0 Own 0 Rent 0
residential_status funded_final
Living with parents 0
Rent 0
Rent 0
Own 1
Own 0
Own 0
Rent 0
Rent 0
Rent 0
Living with parents 0
Rent 0
Rent 0
Rent 1
当我在函数外部执行此操作时,效果非常好
test2 %>% group_by(residential_status) %>%
summarise(tar_average = round((mean(funded_final, na.rm=TRUE))*100,2),N = n()) %>% arrange(desc(tar_average)) %>% mutate(Perc = round((N/sum(N))*100,2),Cum_Perc = cumsum(Perc))%>% print(n = nrow(.))
结果是:
residential_status tar_average N Perc Cum_Perc
<fctr> <dbl> <int> <dbl> <dbl>
1 Own 33.33 3 23.08 23.08
2 Rent 12.50 8 61.54 84.62
3 Living with parents 0.00 2 15.38 100.00
居住状态平均百分比
1拥有33.33 3 23.08 23.08
2租金12.50861.5484.62
3与父母同住0.00 2 15.38 100.00
当我使用该函数时,我只得到总平均值:
group.by.func <- function(dataframe,target){ dataframe %>%group_by(residential_status) %>%
summarise(tar_average = round((mean(target, na.rm=TRUE))*100,2),N = n()) %>%
arrange(desc(tar_average)) %>%
mutate(Perc = round((N/sum(N))*100,2),Cum_Perc = cumsum(Perc))%>%
print(n = nrow(.))}
group.by.func(test2,test2$funded_final)
group.by.func%group\u by(居住状态)%>%
总结(tar_average=round((平均值(target,na.rm=TRUE))*100,2),N=N())%>%
排列(描述(焦油平均值))%>%
变异(Perc=round((N/sum(N))*100,2),Cum_Perc=cumsum(Perc))%>%
打印(n=nrow(.))}
group.by.func(test2,test2$final)
结果:
residential_status tar_average N Perc Cum_Perc
<fctr> <dbl> <int> <dbl> <dbl>
1 Living with parents 15.38 2 15.38 15.38
2 Own 15.38 3 23.08 38.46
3 Rent 15.38 8 61.54 100.00
# A tibble: 3 × 5
residential_status tar_average N Perc Cum_Perc
<fctr> <dbl> <int> <dbl> <dbl>
1 Own 33.33 3 23.08 23.08
2 Rent 12.50 8 61.54 84.62
3 Living with parents 0.00 2 15.38 100.00
居住状态平均百分比
1与父母同住15.38 2 15.38 15.38
2拥有15.38 3 23.08 38.46
3租金15.38861.54100.00
提前谢谢 问题在于
dplyr::summary
使用非标准求值,并希望列的名称为不带引号的字符串。在您的例子中,变量target
不是列名,而是包含列值的向量。该函数无法将向量与data.frame关联。因此,分组不适用于向量目标
。在对分组的data.frame的每次评估中,对整个向量target
取平均值
您可以通过将列名作为字符串传递并使用“标准评估”版本的dplyr::summary
:
group.by.func <- function(dataframe, target){
dataframe %>% group_by(residential_status) %>%
summarise_(.dots = list(
tar_average = paste0("round((mean(", target,", na.rm=TRUE))*100,2)"),
N = "n()")) %>%
arrange(desc(tar_average)) %>%
mutate(Perc = round((N/sum(N))*100,2),Cum_Perc = cumsum(Perc))%>%
print(n = nrow(.))
}
group.by.func(test2,"funded_final")
group.by.func%group\u by(居住状态)%>%
总结(点=列表)(
tar_average=paste0(“圆形((平均值(,目标值,,na.rm=TRUE))*100,2”),
N=“N()”)%%>%
排列(描述(焦油平均值))%>%
变异(Perc=round((N/sum(N))*100,2),Cum_Perc=cumsum(Perc))%>%
打印(n=nrow()
}
group.by.func(测试2,“最终资助”)
结果:
residential_status tar_average N Perc Cum_Perc
<fctr> <dbl> <int> <dbl> <dbl>
1 Living with parents 15.38 2 15.38 15.38
2 Own 15.38 3 23.08 38.46
3 Rent 15.38 8 61.54 100.00
# A tibble: 3 × 5
residential_status tar_average N Perc Cum_Perc
<fctr> <dbl> <int> <dbl> <dbl>
1 Own 33.33 3 23.08 23.08
2 Rent 12.50 8 61.54 84.62
3 Living with parents 0.00 2 15.38 100.00
#一个tible:3×5
居住状况平均每平方米
1拥有33.33 3 23.08 23.08
2租金12.50861.5484.62
3与父母同住0.00 2 15.38 100.00
Great!:)非常感谢@ikopHi@ikop,如果我需要在我的函数中再加一个参数(例如居住状态),我应该这样做吗?对不起,我对这些东西有点困惑。谢谢在函数中定义第二个目标参数(例如target1
和target2
)。在对摘要的调用中
,使用粘贴构建评估字符串,并将其作为附加列表元素添加到.dots
中。非常感谢@ikop