# Syntax for a repeated measures ANOVA in R

I am trying to conduct a repeated measures ANOVA on the following data:

id     pre-treatment     post-treatment
1      212               208
2      192               189
3      167               168
4      151               151
5      135               128
6      105               118
7      88                88
8      67                71
9      55                50
10     27                30


An online repeated measures anova calculator tells me that the p-value of the above repeated measures anova is: 0.914259. I agree that this is correct.

But when I try this in R, I use the following syntax:

model <- aov( post-treatment ~ pre-treatment + Error(id), data = df.anova )
print(summary(model))


And I get the following output:

Error: id
Df Sum Sq Mean Sq
pre  1   1448    1448

Error: Within
Df Sum Sq Mean Sq F value Pr(>F)
pre         1 702527  702527   26248 <2e-16 ***
Residuals 217   5808      27
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>


But p < 2e-16 is a lot different from p = 0.914259

What gives? I'm sure something is wrong with my syntax about how I'm running the ANOVA with the aov function. There are many tutorials online about how to do repeated measures ANOVA in R. But most of them either

• spend 15 pages explaining ANOVA and repeated-measures ANOVA like I've never heard of ANOVA

or they

• have a 7 or 8 page example using the mtcars dataset that leave me unable to see the forest for the trees.

Can anyone advise me on how to do repeated measures ANOVA in R?

The factor is the treatment. You have to use such a dataset:

   id   y treatment
1   1 212       pre
2   2 192       pre
3   3 167       pre
4   4 151       pre
5   5 135       pre
6   6 105       pre
7   7  88       pre
8   8  67       pre
9   9  55       pre
10 10  27       pre
11  1 208      post
12  2 189      post
13  3 168      post
14  4 151      post
15  5 128      post
16  6 118      post
17  7  88      post
18  8  71      post
19  9  50      post
20 10  30      post


dat0 <- read.table(text = "id     pre     post
1      212               208
2      192               189
3      167               168
4      151               151
5      135               128
6      105               118
7      88                88
8      67                71
9      55                50
10     27                30", header = TRUE)

dat0$$id <- factor(dat0$$id)

dat <- rbind(
data.frame(id = dat0$$id, y = dat0$$pre),
data.frame(id = dat0$$id, y = dat0$$post)
)
dat\$treatment <- gl(2, 10, labels = c("pre", "post"))

summary(aov(y ~ treatment + Error(id), data = dat))


Error: Within
Df Sum Sq Mean Sq F value Pr(>F)
treatment  1    0.2    0.20   0.012  0.914
Residuals  9  146.8   16.31