Search code examples
rdplyrmagrittr

Can you pipe data into a pairwise.t.test?


I'm wondering if the following code can be simplified to allow the data to be piped directly from the summarise command to the pairwise.t.test, without creating the intermediary object?

data_for_PTT <- data %>% 
  group_by(subj, TT) %>% 
  summarise(meanRT = mean(RT))

pairwise.t.test(x = data_for_PTT$meanRT, g = data_for_PTT$TT, paired = TRUE)

I tried x = .$meanRT but it didn't like it, returning:

Error in match.arg(p.adjust.method) : 'arg' must be NULL or a character vector


Solution

  • You can use curly braces:

    data_for_PTT <- data %>% 
      group_by(subj, TT) %>% 
      summarise(meanRT = mean(RT)) %>%
      {pairwise.t.test(x = .$meanRT, g = .$TT, paired = TRUE)}
    

    Reproducible:

    df <- data.frame(X1 = runif(1000), X2 = runif(1000), subj = rep(c("A", "B")))
    
    df %>% 
      {pairwise.t.test(.$X1, .$subj, paired = TRUE)}