【问题标题】:dplyr is not working inside functiondplyr 在函数内部不起作用
【发布时间】:2017-03-24 10:54:26
【问题描述】:

我对 R 中的函数没有经验。我正在尝试构建一个通过目标变量计算平均值的函数(在我的示例中:funded_final)。

我的数据:

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

当我使用该函数时,我只得到总平均值:

 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)

结果:

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

提前致谢!

【问题讨论】:

    标签: r function dplyr


    【解决方案1】:

    问题在于dplyr::summarise 使用非标准评估并期望列的名称为不带引号的字符串。在您的情况下,变量 target 不是列名,而是包含列值的向量。该函数无法将向量与 data.frame 相关联。因此,分组不适用于向量target。在分组数据帧的每次评估中,均值取自整个向量target

    您可以通过将列名作为字符串传递并使用dplyr::summarise 的“标准评估”版本来解决它:

    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")
    

    结果:

    # 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
    

    【讨论】:

    • 太棒了! :) 非常感谢@ikop
    • 嗨@ikop,如果我需要在我的函数中再添加一个参数(例如居住状态),我应该这样做吗?对不起,我对这个 NSE 的东西有点困惑。谢谢
    • 只需在函数中定义第二个目标参数(例如target1target2)。在您对summarise_ 的调用中,使用paste 构造您的评估字符串,并将其作为附加列表元素添加到.dots
    • 非常感谢@ikop
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多