1

I'm using this pretty nice code to perform a multiple t.test keeping the independent variable constant!

data(mtcars)
library(dplyr)
vars_to_test <- c("disp","hp","drat","wt","qsec")
iv <- "vs"
mtcars %>%
  summarise_each_(
    funs_( 
      sprintf("stats::t.test(.[%s == 0], .[%s == 1])$p.value",iv,iv)
    ), 
    vars = vars_to_test)

Unfortunately, dplyr was updated and I've been facing this report

summarise_each() is deprecated. Use summarise_all(), summarise_at() or summarise_if() instead. To map funs over a selection of variables, use summarise_at()

When i change the code for _all, at or _if, this function doest not work any more. I'm looking for some advice and thanks much for your support.

Thanks

Luis
  • 1,388
  • 10
  • 30

1 Answers1

1

Instead of creating a string expression with sprintf and then evaluating it, we can use the evaluate the 'vs' converting it to symbol and then evaluate it

library(dplyr)
mtcars %>%
   summarise_at(vars(vars_to_test), funs( 
     try(stats::t.test(.[(!! rlang::sym(iv)) == 0], .[(!! rlang::sym(iv)) == 1])$p.value)
  ))
#          disp           hp       drat           wt         qsec
#1 2.476526e-06 1.819806e-06 0.01285342 0.0007281397 3.522404e-06

If we really wanted to parse an expression, use the rlang_parse_expr and rlang::eval_tidy along with sym

library(rlang)
eval_tidy(parse_expr("mtcars %>% summarise_at(vars(vars_to_test), 
                funs(t.test(.[(!!sym(iv))==0],
                                   .[(!!sym(iv))==1])$p.value ))"))
#          disp           hp       drat           wt         qsec
#1 2.476526e-06 1.819806e-06 0.01285342 0.0007281397 3.522404e-06
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks, @akrun. Could you please provide a code that i could replace this t.test to a t.test using ~ function ? This t.test is giving a result considering two continuous variables and my goal is a continuous dv in two groups (categorical) – Luis Nov 19 '17 at 19:34
  • @Luis It is not clear from your comments. Could you post as a new question. – akrun Nov 19 '17 at 19:37
  • Thanks. Yes, this code solved my question. I'll click there! @akrun – Luis Nov 20 '17 at 02:10