使用带有多个参数的函数的dplyr :: across [英] using `dplyr::across` with functions with more than one argument
问题描述
我想知道是否有一种方法可以将 dplyr :: across
与需要多个参数的函数一起使用,如果没有,那么如何在 dplyr中完成以下操作
/ tidyverse
.
I am wondering if there is a way to use dplyr::across
with a function that requires multiple arguments, and, if not, how can the following be done in dplyr
/tidyverse
.
library(dplyr)
# create a dataframe
df <-
structure(list(
x1_estimate = c(
0.185050288587259, 0.151839113724119,
0.134106347795535, 0.16816621423223
), x2_estimate = c(
0.210983518279099,
0.337090844267208, 0.324663150698154, 0.254871197876221
), x3_estimate = c(
0.122881208643618,
0.0707293652735489, 0.0981291893590288, -0.0214831044826657
),
x1_se = c(
0.00986950954467025, 0.00625871919316588, 0.0445182168165812,
0.0244314083271791
), x2_se = c(
0.00954593822897476, 0.00669845532512913,
0.0478789857255503, 0.0237263111649421
), x3_se = c(
0.017952784431167,
0.0122226237123911, 0.0836135673502282, 0.041558861509543
)
), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"))
对于仅需要单个参数的函数
例如,假设我们只想计算方差,它只需要一个参数(标准误差)
for a function that requires only a single argument
For example, let's say we just want to compute variance, which requires only a single argument (standard error)
df %>% mutate(across(contains("_se"), ~ (.^2), .names = "{.col}_var"))
#> # A tibble: 4 x 9
#> x1_estimate x2_estimate x3_estimate x1_se x2_se x3_se x1_se_var x2_se_var
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.185 0.211 0.123 0.00987 0.00955 0.0180 0.0000974 0.0000911
#> 2 0.152 0.337 0.0707 0.00626 0.00670 0.0122 0.0000392 0.0000449
#> 3 0.134 0.325 0.0981 0.0445 0.0479 0.0836 0.00198 0.00229
#> 4 0.168 0.255 -0.0215 0.0244 0.0237 0.0416 0.000597 0.000563
#> # … with 1 more variable: x3_se_var <dbl>
对于需要多个参数的函数
现在假设我们要计算置信区间,这需要估计值和进行计算.
x1_conf.low = x1_estimate - 1.96 * x1_se
x2_conf.low = x2_estimate - 1.96 * x2_se
x3_conf.low = x3_estimate - 1.96 * x3_se
我知道,这是行不通的,但这只是出于说明目的:
I know that this won't work, but it's just for illustrative purposes:
df %>%
mutate(
across(matches("_se|_estimate"),
~ (contains("_estimate") - 1.96 * contains("_se")),
.names = "{.col}_conf.low"
)
)
#> Error: Problem with `mutate()` input `..1`.
#> x `contains()` must be used within a *selecting* function.
#> ℹ See <https://tidyselect.r-lib.org/reference/faq-selection-context.html>.
#> ℹ Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`.
如果没有,我也很高兴看到使用 dplyr
/ tidyverse
的另一种解决方案,以实现相同的目的.
If not, I would also be happy to see another solution with dplyr
/tidyverse
to achieve the same.
推荐答案
您可以将 cross
用作:
library(dplyr)
df %>%
mutate(across(contains("_estimate"), .names = "{.col}_conf.low") -
1.96 * across(contains("_se")))
在基数R中,您可以执行以下操作:
In base R, you can do :
estimate_cols <- grep('estimate', names(df), value = TRUE)
se_cols <- grep('se', names(df), value = TRUE)
df[paste0(estimate_cols, '_conf.low')] <- df[estimate_cols] - 1.96 * df[se_cols]
这篇关于使用带有多个参数的函数的dplyr :: across的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!