问题描述
我想将列插入通过自定义内部函数执行purrr::imap_dfr
的函数中。
我的目标是df %>% diffmean(df,group,col1,col2)
将运行t.test(col1 ~ group,.data = df)
和t.test(col2 ~ group,.data = df
。
ttests <- function(df,...) {
group <- rlang::ensym(group)
vars <- rlang::ensyms(...)
df %>%
dplyr::select(c(!!!vars)) %>%
purrr::imap_dfr(function(.x,.y) {
broom::tidy(t.test(.x ~ !!group)) %>%
dplyr::mutate(name = .y) %>%
dplyr::select(name,dplyr::everything())
})
}
如果我只是在要分组的!!group
列中简单地硬编码,并且如果我想用!!!vars
切换出要选择的变量,则以上代码将起作用。
我只想将此泛型用于将来使用。
例如,使用diamonds
中的ggplot2
数据集:
diamonds <- diamonds %>%
dplyr::mutate(carat = carat > 0.25)
diamonds %>%
dplyr::select(depth,table,price,x,y,z) %>%
purrr::imap_dfr(.,function(.x,.y) {
broom::tidy(t.test(.x ~ diamonds$carat)) %>%
dplyr::mutate(name = .y) %>%
dplyr::select(name,dplyr::everything())
})
产生:
name estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 depth -0.247 61.5 61.8 -4.86 0.00000143 808. -0.347 -0.147 Welch Two Sample t-test two.sided
2 table 0.263 57.7 57.5 3.13 0.00183 805. 0.0977 0.427 Welch Two Sample t-test two.sided
3 price -3477. 506. 3983. -197. 0 51886. -3512. -3443. Welch Two Sample t-test two.sided
4 x -1.77 3.99 5.76 -299. 0 6451. -1.78 -1.76 Welch Two Sample t-test two.sided
5 y -1.75 4.01 5.76 -290. 0 6529. -1.76 -1.73 Welch Two Sample t-test two.sided
6 z -1.10 2.46 3.55 -294. 0 6502. -1.10 -1.09 Welch Two Sample t-test two.sided
解决方法
R t.test
基本语法实际上并不是设计用于rlang
样式语法的,因此您需要对公式进行一些摸索。这应该工作
ttests <- function(df,group,...) {
group <- rlang::ensym(group)
vars <- rlang::ensyms(...)
df %>%
dplyr::select(c(!!!vars)) %>%
purrr::imap_dfr(function(.x,.y) {
rlang::eval_tidy(rlang::quo(t.test(!!rlang::sym(.y) ~ !!group,df))) %>%
broom::tidy() %>%
dplyr::mutate(name = .y) %>%
dplyr::select(name,dplyr::everything())
})
}
基本上,我们正在构建表达式t.test(val ~ group,df)
,然后对其求值。
这适用于示例输入
ggplot2::diamonds %>%
dplyr::mutate(carat = carat > 0.25) %>%
ttests(carat,depth,table,price,x,y,z)
# name estimate estimate1 estimate2 statistic p.value parameter conf.low
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 depth -2.47e-1 61.5 61.8 -4.86 1.43e-6 808. -3.47e-1
# 2 table 2.63e-1 57.7 57.5 3.13 1.83e-3 805. 9.77e-2
# 3 price -3.48e+3 506. 3983. -197. 0. 51886. -3.51e+3
# 4 x -1.77e+0 3.99 5.76 -299. 0. 6451. -1.78e+0
# 5 y -1.75e+0 4.01 5.76 -290. 0. 6529. -1.76e+0
# 6 z -1.10e+0 2.46 3.55 -294. 0. 6502. -1.10e+0
,
还可以选择转换为'long'格式,然后在进行nest_by
library(dplyr)
library(tidyr)
ttests <- function(df,...) {
grp <- rlang::as_name(ensym(group))
df %>%
dplyr::select(!!! enquos(...),grp) %>%
pivot_longer(cols = -grp) %>%
nest_by(name) %>%
transmute(name,new = list(broom::tidy(t.test(reformulate(grp,response = 'value'),data)))) %>%
unnest_wider(c(new))
}
ttests(diamonds,carat,z)
# A tibble: 6 x 11
# name estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
#1 depth -0.247 61.5 61.8 -4.86 0.00000143 808. -0.347 -0.147 Welch Two Sample t-test two.sided
#2 price -3477. 506. 3983. -197. 0 51886. -3512. -3443. Welch Two Sample t-test two.sided
#3 table 0.263 57.7 57.5 3.13 0.00183 805. 0.0977 0.427 Welch Two Sample t-test two.sided
#4 x -1.77 3.99 5.76 -299. 0 6451. -1.78 -1.76 Welch Two Sample t-test two.sided
#5 y -1.75 4.01 5.76 -290. 0 6529. -1.76 -1.73 Welch Two Sample t-test two.sided
#6 z -1.10 2.46 3.55 -294. 0 6502. -1.10 -1.09 Welch Two Sample t-test two.sided