gpt4 book ai didi

r - dplyr NSE 中的多个列名

转载 作者:行者123 更新时间:2023-12-05 03:05:51 25 4
gpt4 key购买 nike

我正在编写函数来自动化分析大量人口统计数据的工作流程。我可以从 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

但我不能同时向 ... 提供 townregion:

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

reprex package 创建于 2018-05-08 (v0.2.0).

提前致谢!

最佳答案

我们可以用 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

对于单列的情况,我们需要选择'region'或'town',因为它也是数据集中的一列(或者需要在函数中更改)

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

关于r - dplyr NSE 中的多个列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50238648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com