将数据框的列传递给内部函数

Pass column of data frame to inner function

我想将列通过管道传输到一个函数中,该函数使用自定义内部函数执行 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, ...) {
  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 分组的列中简单地硬编码,并且如果我切换我想要 select 和 !!!vars 的变量,上面的代码就可以工作.

我只是想让这个通用以供将来使用。

例如,使用来自 ggplot2diamonds 数据集:

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 命令并不是真正设计用于 rlangstyle 语法,因此您需要对公式进行一些修改。这应该有效

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, group, ...) {
   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, depth, table, price, x, y, 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