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