Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将数据帧的列传递给内部函数_R_Tidyeval_Non Standard Evaluation - Fatal编程技术网

R 将数据帧的列传递给内部函数

R 将数据帧的列传递给内部函数,r,tidyeval,non-standard-evaluation,R,Tidyeval,Non Standard Evaluation,我希望将列中的管道插入到一个函数中,该函数使用自定义内部函数执行purr::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(...)

我希望将列中的管道插入到一个函数中,该函数使用自定义内部函数执行
purr::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())
  })
}
产生:

  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 
name estimate1 estimate2统计p.value参数conf.low conf.high方法备选方案
1深度-0.247 61.5 61.8-4.86 0.00000143 808。-0.347-0.147韦尔奇双面双样本t检验
表0.263 57.7 57.5 3.13 0.00183 805.0.0977 0.427韦尔奇双样本t检验双侧
3价格-3477.506.3983.-197.0 51886.-3512.-3443.韦尔奇双样本t检验双面
4X-1.773.995.76-299.06451-1.78-1.76韦尔奇双面双样本t检验
5Y-1.754.015.76-290.06529-1.76-1.73韦尔奇双样本t检验双侧
6 z-1.10 2.46 3.55-294.0 6502。-1.10-1.09韦尔奇双面双样本t检验

基本R
t.test
命令并非真正设计用于
rlang
样式的styntax,因此您需要对公式进行一些修改。这应该可以工作

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())
    })
}

基本的R
t.test
命令并不是真正设计用于
rlang
样式的styntax,因此您需要对公式进行一些修改。这应该可以工作

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())
    })
}

还可以选择转换为“长”格式,然后在执行
nest\u 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  
库(dplyr)
图书馆(tidyr)
ttests%
枢轴长度(cols=-grp)%>%
嵌套单位(名称)%>%
蜕变,
新建=列表(扫帚::整洁(t.test(重新格式化(grp,响应='value'),数据)))%>%
unnest_加宽(c(新))
}
测试(钻石、克拉、深度、表、价格、x、y、z)
#一个tibble:6x11
#名称估计估计1估计2统计p.value参数conf.low conf.high方法备选方案
#                                                                      
#1深度-0.247 61.5 61.8-4.86 0.00000143 808。-0.347-0.147韦尔奇双面双样本t检验
#2价格-3477.506.3983.-197.0 51886.-3512.-3443.韦尔奇双样本t检验双面
#表0.263 57.7 57.5 3.13 0.00183 805.0.0977 0.427 Welch双面双样本t检验
#4X-1.773.995.76-299.06451-1.78-1.76韦尔奇双面双样本t检验
#5Y-1.754.015.76-290.06529-1.76-1.73韦尔奇双样本t检验双侧
#6 z-1.10 2.46 3.55-294.0 6502。-1.10-1.09韦尔奇双面双样本t检验

另一个选项是转换为“长”格式,然后在执行
嵌套操作后应用公式

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  
库(dplyr)
图书馆(tidyr)
ttests%
枢轴长度(cols=-grp)%>%
嵌套单位(名称)%>%
蜕变,
新建=列表(扫帚::整洁(t.test(重新格式化(grp,响应='value'),数据)))%>%
unnest_加宽(c(新))
}
测试(钻石、克拉、深度、表、价格、x、y、z)
#一个tibble:6x11
#名称估计估计1估计2统计p.value参数conf.low conf.high方法备选方案
#                                                                      
#1深度-0.247 61.5 61.8-4.86 0.00000143 808。-0.347-0.147韦尔奇双面双样本t检验
#2价格-3477.506.3983.-197.0 51886.-3512.-3443.韦尔奇双样本t检验双面
#表0.263 57.7 57.5 3.13 0.00183 805.0.0977 0.427 Welch双面双样本t检验
#4X-1.773.995.76-299.06451-1.78-1.76韦尔奇双面双样本t检验
#5Y-1.754.015.76-290.06529-1.76-1.73韦尔奇双样本t检验双侧
#6 z-1.10 2.46 3.55-294.0 6502。-1.10-1.09韦尔奇双面双样本t检验

这太好了,谢谢!我还想检查
group
是否是逻辑向量-
rlang::eval_tidy(rlang::quo(is.logical(df$!!group)))
不起作用,因为它不喜欢
df$!!group
。有什么简单的方法来评估它吗?抱歉,仍然在与NSE斗争-试图同时阅读Hadley在Advanced R中的章节。只需使用
dplyr::pull()
动词:
is.logical(df%>%pull(!!group))就更容易了
这太好了,谢谢!我还想检查
group
是否是逻辑向量-
rlang::eval_tidy(rlang::quo(is.logical(df$!!group)))
不起作用,因为它不喜欢
df$!!group
。有什么简单的方法来评估这个问题吗?对不起,仍然在为NSE而挣扎-试图同时阅读Hadley在Advanced R中的章节。只需使用
dplyr::pull()
动词:
is.logical(df%>%pull(!!group))