gpt4 book ai didi

r - 我怎样才能编写一个 tidyverse 友好的函数,在管道的早期尊重 group_by() ?

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

我已经开始着手编写函数来加快表的生成速度,但我想让该函数尊重用户在管道中所做的早期分组选择。

示例数据:

df<-data.frame(ID=c("A","B","C","A","C","D","A","C","E","B","C","A"),
Year=c(1,1,1,2,2,2,3,3,3,4,4,4),
Credits=c(1,3,4,5,6,7,2,1,1,6,1,2),
Major=c("GS","GS","LA","GS","GS","LA","GS","LA","LA","GS","LA","LA"),
Status=c("green","blue","green","blue","green","blue","green","blue","green","blue","green","blue"),
Group=c("Art","Music","Science","Art","Music","Science","Art","Music","Science","Art","Music","Science"))

以下是我正在处理的函数,它需要/接受一个变量来定义队列、一个信用变量和一个术语变量。

table_headsfte_cohorts<-function(.data,cohortvar,credits,term){


cohortvar<-rlang::ensym(cohortvar)
credits<-rlang::ensym(credits)
term<-rlang::ensym(term)


.data%>%
group_by(!!term,Pidm)%>%
group_by(!!term,!!cohortvar,group_cols())%>%
mutate(on3=1)%>%
mutate(`Headcount`=sum(on3),
`FTE`=round(sum(na.omit(!!credits))/15,1))%>%
mutate(Variable=paste0(cohortvar))%>%
mutate(Category=!!cohortvar)%>%
select(-!!cohortvar)%>%
select(Variable,Category,Headcount,FTE,group_cols())
}

对于可能有兴趣在他们选择的同类群组变量之外使用其他分组变量的用户,我希望最终结果函数允许按如下方式使用:

df2<-df%>%
group_by(Status,Group)%>%
table_headsfte_cohorts(Major,Credits,Year)

除了 cohortvar 之外,期望的最终结果将是一个尊重并保留上述 group_by 语句中两个分组变量水平的表格来自 table_headsfte_cohorts() 参数的 term 列。

我需要生成同一张表,但对于范围广泛的分组变量和不同数量的分组变量,因此灵 active 将非常有帮助。

编辑:

通过至少允许多个分组变量,以下内容似乎接近了。这不是我所希望的,因为我更希望从管道中读取额外的分组参数:

 table_headsfte_cohorts<-function(.data,cohortvar,credits,term,...){

grps<-enquos(...)

cohortvar<-rlang::ensym(cohortvar)
credits<-rlang::ensym(credits)
term<-rlang::ensym(term)


.data%>%
group_by(!!term,!!cohortvar,!!! grps)%>%
mutate(on3=1)%>%
mutate(`Headcount`=sum(on3),
`FTE`=round(sum(na.omit(!!credits))/15,1))%>%
mutate(Variable=paste0(cohortvar))%>%
mutate(Category=!!cohortvar)%>%
select(-!!cohortvar)%>%
select(Variable,Category,Headcount,FTE,!!!grps)

使用上面的,我可以成功运行:

fdfout<-fdf%>%
table_headsfte_cohorts(Major, Credits, Year), getting:

enter image description here

我还可以将其他变量传递给函数以用作额外的分组变量:

fdfout_alt<-fdf%>%
table_headsfte_cohorts(Major,Credits,Year,Status,Group)

产生期望的结果:

enter image description here

不幸的是,当我使用

fdf_no<-fdf%>%
group_by(Status, Group)%>%
table_headsfte_cohorts(Major, Credits, Year)

我得到:

enter image description here

此输出可能会使使用我的函数的人感到困惑,因为他们的 group_by() 行似乎什么都不做。

最佳答案

我添加了一些行,将点内的现有分组变量和新分组变量合并到一个字符向量中。我们可以通过 group_vars 获取现有的分组变量。要将新旧合并在一起,我们必须获取引用分组变量的表达式 get_expr 并将它们转换为字符串。我们可以使用 !!! syms 评估和 all_of 选择分组变量。

这是你的想法吗?

table_headsfte_cohorts <- function(.data, cohortvar, credits, term, ...){

new_grps <- enquos(...)
new_grps <- purrr::map_chr(new_grps, ~ as.character(rlang::get_expr(.x)))
ex_grps <- group_vars(.data)
grp_vars <- c(ex_grps, new_grps)

cohortvar<-rlang::ensym(cohortvar)
credits<-rlang::ensym(credits)
term<-rlang::ensym(term)


.data%>%
group_by(!! term,
!! cohortvar,
!!! syms(grp_vars))%>%
mutate(on3 = 1) %>%
mutate(`Headcount`= sum(on3),
`FTE`= round(sum(na.omit(!!credits))/15,1))%>%
mutate(Variable=paste0(cohortvar))%>%
mutate(Category=!!cohortvar)%>%
select(-!!cohortvar)%>%
select(Variable,Category,Headcount,FTE, all_of(grp_vars))

}

df %>%
group_by(Status, Group) %>%
table_headsfte_cohorts(Major, Credits, Year)

#> Adding missing grouping variables: `Major`
#> Adding missing grouping variables: `Year`, `Major`
#> # A tibble: 12 x 8
#> # Groups: Year, Major, Status, Group [12]
#> Year Major Variable Category Headcount FTE Status Group
#> <dbl> <chr> <chr> <chr> <dbl> <dbl> <chr> <chr>
#> 1 1 GS Major GS 1 0.1 green Art
#> 2 1 GS Major GS 1 0.2 blue Music
#> 3 1 LA Major LA 1 0.3 green Science
#> 4 2 GS Major GS 1 0.3 blue Art
#> 5 2 GS Major GS 1 0.4 green Music
#> 6 2 LA Major LA 1 0.5 blue Science
#> 7 3 GS Major GS 1 0.1 green Art
#> 8 3 LA Major LA 1 0.1 blue Music
#> 9 3 LA Major LA 1 0.1 green Science
#> 10 4 GS Major GS 1 0.4 blue Art
#> 11 4 LA Major LA 1 0.1 green Music
#> 12 4 LA Major LA 1 0.1 blue Science

关于r - 我怎样才能编写一个 tidyverse 友好的函数,在管道的早期尊重 group_by() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66321379/

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