1

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

neilfws
  • 32,751
  • 5
  • 50
  • 63
  • [This question and answer](https://stackoverflow.com/questions/53110899/r-dplyr-using-pipes-to-select-data-and-conduct-t-tests-with-data-from-outside) seems to address a very similar problem. – neilfws Feb 12 '20 at 21:32
  • yes I saw that, but there are two differences, one being the use of columns across two dataframes, and the other being the use of t.test, rather than pairwise.t.test. The latter does not allow the "data" parameter, which seemed key. I'm probably wrong, but I couldn't figure out how to apply the advice given there. – Tom Beesley Feb 12 '20 at 21:36

1 Answers1

3

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)}
Greg
  • 3,570
  • 5
  • 18
  • 31
  • Brilliant. that works. Novice question - For future reference, what is the function of the curly braces here? – Tom Beesley Feb 12 '20 at 21:48
  • 1
    The contents of curly braces are evaluated in their current environment, which here includes whatever is being piped in. Calling a function (like ```pairwise.t.test```) creates a new environment, which may or may not play nicely with the pipe. The curly braces force the called function to work in an environment that contains whatever you pipe in. – Greg Feb 12 '20 at 22:02
  • If someone else wants to take a whack at that explanation it wouldn't be the worst idea... – Greg Feb 12 '20 at 22:06