我正在使用
dplyr
包和其他相关包(例如 tidyr
和 tidyselect
)编写一个函数。在阅读了使用 dplyr 进行编程以及多次尝试和错误之后,我能够编写一个包含数据屏蔽/整齐选择和名称注入的函数来实现部分目标。
compute_ratio <- function(numerator = dcdata,
denominator = pgdata,
bygroup = c('STUDY', 'SITE'),
nbydp = n_dc_bydp_ym,
numsuffix = dc_byss,
denosuffix = pg_byss){
numerator %>%
dplyr::group_by(dplyr::pick({{ bygroup }})) %>%
dplyr::summarise("n_{{numsuffix}}" := sum({{ nbydp }}, na.rm = TRUE)) %>%
dplyr::ungroup() %>%
# Merge denominator data with numerator data
dplyr::full_join(denominator, by = {{ bygroup }}) %>%
dplyr::mutate("cum_n_{{denosuffix}}" := cumsum("n_{{numsuffix}}"),
"ratio_{{numsuffix}}" := dplyr::if_else("cum_n_{{denosuffix}}" == 0, NA,
round("n_{{numsuffix}}" / "cum_n_{{denosuffix}}", 3)))
}
当前问题在最后
mutate()
。我无法弄清楚如何引用同一函数的前几行中的名称注入名称。例如,"n_{{numsuffix}}"
是从summarise()
派生的数据变量。当我稍后需要在最后一个mutate()
引用这个数据变量时,它现在不在 := 的 LHS 上,而是在 := 的 RHS 上。
如有任何建议或指导,我们将不胜感激。如何稍后在同一用户定义函数中将先前的名称注入作为数据变量引用?
我尝试过
.data[[]]
,拥抱{{ }}
,!!enquo
。一切都以错误结束。
分子数据样本 -
dcdata
structure(list(STUDY = c("A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B"),
SITE = c("0186", "0187", "0713", "3205", "3403", "4115",
"4304", "4407", "4413", "4603", "6140", "8617", "8618", "061027",
"086009", "086020", "086048", "086108", "086124", "086344"
), n_pg_byss = c(2705L, 371L, 1495L, 404L, 1357L, 2089L,
456L, 886L, 830L, 5034L, 912L, 1739L, 1991L, 60L, 18L, 858L,
666L, 759L, 28L, 171L)), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
分母数据样本 --
pgdata
structure(list(STUDY = c("A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B"),
SITE = c("0186", "0187", "0713", "3205", "3403", "4115",
"4304", "4407", "4413", "4603", "6140", "8617", "8618", "061027",
"086009", "086020", "086048", "086108", "086124", "086344"
), n_pg_byss = c(2705L, 371L, 1495L, 404L, 1357L, 2089L,
456L, 886L, 830L, 5034L, 912L, 1739L, 1991L, 60L, 18L, 858L,
666L, 759L, 28L, 171L)), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
测试功能
test_ratiofun <- compute_ratio(numerator = dcdata,
denominator = pgdata,
bygroup = c('STUDY', 'SITE'),
nbydp = n_dc_bydp_ym,
numsuffix = dc_byss,
denosuffix = pg_byss)
也许有一种不太复杂的方法,但似乎适合你想要的
library(tidyverse)
library(rlang)
library(glue)
myfunc <- function(x,numsuffix,denosuffix){
n_text <- as.character(glue("n_{as_name(enquo(numsuffix))}"))
group_by(x,
Species) |>
summarise("{n_text}":= sum(Petal.Length, na.rm = TRUE)) %>%
dplyr::ungroup() |>
mutate(
"cum_n_{{denosuffix}}" := cumsum(!!sym(n_text)))
}
myfunc(iris,whatever,also)
# A tibble: 3 × 3
Species n_whatever cum_n_also
<fct> <dbl> <dbl>
1 setosa 73.1 73.1
2 versicolor 213 286.
3 virginica 278. 564.