4

I have seen very similar questions to this but still can't figure this simple problem out.

I want to run paired t-tests over a grouping variable and store the results in a dataframe. The command tidy in package broom does this but when I run the code below the output is the same for each time point. I know I could do it using a loop but I want to use tidy here and understand what is happening.

Here is a reproducible example:

library(dplyr)
library(broom)
df <- data.frame (time1=rep(1:4,30),
              sbp=runif(120, 100, 150),
              sbp1=runif(120, 120, 170))

R>head(df)
  time1      sbp     sbp1
1     1 146.9411 151.4842
2     2 102.7216 139.3380
3     3 125.7126 167.1806
4     4 126.0086 146.2177
5     1 149.9213 139.7968
6     2 117.6843 135.2726   



 z<-df %>%
 arrange(time1) %>%
 group_by(time1) %>%
 do(tidy(t.test(df$sbp,df$sbp1,paired=TRUE)))         

The resulting output is the same for each time point:

R>head(z)
Source: local data frame [4 x 7]
Groups: time1 [4]

  time1  estimate statistic      p.value parameter  conf.low conf.high
  (int)     (dbl)     (dbl)        (dbl)     (dbl)     (dbl)     (dbl)
1     1 -22.48413 -11.69648 1.660608e-21       119 -26.29047 -18.67779
2     2 -22.48413 -11.69648 1.660608e-21       119 -26.29047 -18.67779
3     3 -22.48413 -11.69648 1.660608e-21       119 -26.29047 -18.67779
4     4 -22.48413 -11.69648 1.660608e-21       119 -26.29047 -18.67779

I'm sure there is an easy fix but not sure what it is, any suggestions would be great. Thanks

user63230
  • 4,095
  • 21
  • 43

1 Answers1

6

If you want to refer to the dataset used internally in dplyr, you need to use .:

z <- df %>%
       arrange(time1) %>%
       group_by(time1) %>%
       do(tidy(t.test(.$sbp, .$sbp1,paired=TRUE)))     
Source: local data frame [4 x 7]
Groups: time1 [4]

  time1  estimate statistic      p.value parameter  conf.low conf.high
  (int)     (dbl)     (dbl)        (dbl)     (dbl)     (dbl)     (dbl)
1     1 -22.45646 -6.339261 6.307292e-07        29 -29.70157 -15.21135
2     2 -19.85310 -4.346528 1.550448e-04        29 -29.19485 -10.51136
3     3 -21.00503 -5.996117 1.609850e-06        29 -28.16968 -13.84037
4     4 -23.56341 -6.037170 1.438441e-06        29 -31.54606 -15.58077

By referring to df$sbp you ignoring the grouping done by dplyr and use to complete dataset each time. This explains why the fitted t test is the same each time.

akrun
  • 874,273
  • 37
  • 540
  • 662
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149