dplyr NSE中的多个列名称为quos

时间:2018-05-08 16:50:50

标签: r dplyr nse

我正在编写功能来自动化分析大量人口统计数据的工作流程。我可以从dplyr函数的常规管道流中获得我需要的东西,但是我需要将它抽象为NSE函数。我通过gather参数为一系列...调用提供列名,但这仅适用于单个列;我需要使用多列的选项。我在这种情况下如何使用quos(...)时遇到问题。

该功能还有更多内容,但我已经足够包括显示错误了。

数据样本:

library(tidyverse)

race_pops <- structure(list(
    town = c("Hamden", "Hamden", "Hamden", "Hamden","New Haven", "New Haven", "New Haven", "New Haven", "West Haven","West Haven", "West Haven", "West Haven"), 
    race = c("Total","White", "Black", "Latino", "Total", "White", "Black", "Latino","Total", "White", "Black", "Latino"), 
    est = c(61476, 37043, 13209,6450, 130405, 40164, 42970, 37231, 54972, 28864, 10677, 10977), 
    moe = c(31, 1039, 998, 879, 60, 1395, 1383, 1688, 42, 1226,1119, 1032), 
    region = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,2L, 1L, 1L, 1L, 1L), .Label = c("Inner Ring", "New Haven"), class = "factor")), 
    class = c("tbl_df","tbl", "data.frame"), row.names = c(NA, -12L))

这是一个产生我想要的输出的工作位:

race_pops %>%
    gather(key = measure, value = value, est, moe) %>%
    unite("grp2", race, measure, sep = "_") %>%
    spread(key = grp2, value = value) %>%
    gather(key = grp2, value = value, -town, -region, -starts_with("Total")) %>%
    head(10)
#> # A tibble: 10 x 6
#>    town       region     Total_est Total_moe grp2       value
#>    <chr>      <fct>          <dbl>     <dbl> <chr>      <dbl>
#>  1 Hamden     Inner Ring     61476        31 Black_est  13209
#>  2 New Haven  New Haven     130405        60 Black_est  42970
#>  3 West Haven Inner Ring     54972        42 Black_est  10677
#>  4 Hamden     Inner Ring     61476        31 Black_moe    998
#>  5 New Haven  New Haven     130405        60 Black_moe   1383
#>  6 West Haven Inner Ring     54972        42 Black_moe   1119
#>  7 Hamden     Inner Ring     61476        31 Latino_est  6450
#>  8 New Haven  New Haven     130405        60 Latino_est 37231
#>  9 West Haven Inner Ring     54972        42 Latino_est 10977
#> 10 Hamden     Inner Ring     61476        31 Latino_moe   879

这是我收到错误的函数:

gather_grp <- function(df, grp = group, value = est, moe = moe, ...) {
    name_vars <- quos(...)
    grp_var <- enquo(grp)
    value_var <- enquo(value)
    moe_var <- enquo(moe)

    df %>%
        gather(key = measure, value = value, -(!!!name_vars), -(!!grp_var)) %>%
        unite("grp2", !!grp_var, measure, sep = "_") %>%
        spread(key = grp2, value = value) %>%
        gather(key = grp2, value = value, -(!!!name_vars), -starts_with("Total"))
}

如果我删除region并仅使用单个列town,则此功能有效:

race_pops %>%
    select(-region) %>%
    gather_grp(grp = race, value = est, moe = moe, town) %>%
    head(10)
#> # A tibble: 10 x 5
#>    town       Total_est Total_moe grp2       value
#>    <chr>          <dbl>     <dbl> <chr>      <dbl>
#>  1 Hamden         61476        31 Black_est  13209
#>  2 New Haven     130405        60 Black_est  42970
#>  3 West Haven     54972        42 Black_est  10677
#>  4 Hamden         61476        31 Black_moe    998
#>  5 New Haven     130405        60 Black_moe   1383
#>  6 West Haven     54972        42 Black_moe   1119
#>  7 Hamden         61476        31 Latino_est  6450
#>  8 New Haven     130405        60 Latino_est 37231
#>  9 West Haven     54972        42 Latino_est 10977
#> 10 Hamden         61476        31 Latino_moe   879

但我无法向town提供region...

race_pops %>%
    gather_grp(grp = race, value = est, moe = moe, town, region)
#> Error in (~town): 2 arguments passed to '(' which requires 1

James McNellis(v0.2.0)创建于2018-05-08。

提前致谢!

1 个答案:

答案 0 :(得分:3)

我们可以用c换行,它应该可以正常工作

gather_grp <- function(df, grp = group, value = est, moe = moe, ...) {
    name_vars <- quos(...)
    grp_var <- enquo(grp)
    value_var <- enquo(value)
    moe_var <- enquo(moe)


    df %>%
        gather(key = measure, value = value, -c(!!!name_vars), -!!grp_var) %>%
        unite("grp2", !!grp_var, measure, sep = "_") %>%
        spread(key = grp2, value = value) %>%
        gather(key = grp2, value = value, -c(!!!name_vars), -starts_with("Total"))
}

- 运行功能

race_pops %>%
    gather_grp(grp = race, value = est, moe = moe, town, region)
# A tibble: 18 x 6
#   town       region     Total_est Total_moe grp2       value
#   <chr>      <fct>          <dbl>     <dbl> <chr>      <dbl>
# 1 Hamden     Inner Ring     61476        31 Black_est  13209
# 2 New Haven  New Haven     130405        60 Black_est  42970
# 3 West Haven Inner Ring     54972        42 Black_est  10677
# 4 Hamden     Inner Ring     61476        31 Black_moe    998
# 5 New Haven  New Haven     130405        60 Black_moe   1383
# 6 West Haven Inner Ring     54972        42 Black_moe   1119
# 7 Hamden     Inner Ring     61476        31 Latino_est  6450
# 8 New Haven  New Haven     130405        60 Latino_est 37231
# 9 West Haven Inner Ring     54972        42 Latino_est 10977
#10 Hamden     Inner Ring     61476        31 Latino_moe   879
#11 New Haven  New Haven     130405        60 Latino_moe  1688
#12 West Haven Inner Ring     54972        42 Latino_moe  1032
#13 Hamden     Inner Ring     61476        31 White_est  37043
#14 New Haven  New Haven     130405        60 White_est  40164
#15 West Haven Inner Ring     54972        42 White_est  28864
#16 Hamden     Inner Ring     61476        31 White_moe   1039
#17 New Haven  New Haven     130405        60 White_moe   1395
#18 West Haven Inner Ring     54972        42 White_moe   1226

对于单列情况,我们需要select区域&#39;或者&#39; town&#39;因为它也将是数据集中的一列(或者需要在函数中更改)

race_pops %>% 
    dplyr::select(-region) %>% 
    gather_grp(grp = race, value = est, moe = moe, town)