Using Base R

EXAMPLE 3: Performing One-Way ANOVA

Enter the data into R using the c(…) command or load the data into R using read.csv. To run ANOVA in R, the data must be in two columns with one column containing the factor or treatment and the other column containing the value of the response variable.

Suppose we need to enter the data into R. Proceed as follows:

cojet <- c(15.4,12.9,17.2,16.6,19.3)
silistor <- c(17.2,14.3,17.6,21.6,17.5)
cimara <- c(5.5,7.7,12.2,11.4,16.4)
ceramic <- c(11,12.4,13.5,8.9,8.1)
ANOVA_data <- data.frame('Outcome' = c(cojet, silistor, cimara, ceramic), 'Treatment' = as.factor(rep(c('Cojet', 'Silistor', 'Cimara', 'Ceramic Repair'), each = 5)))
print(ANOVA_data)
##    Outcome      Treatment
## 1     15.4          Cojet
## 2     12.9          Cojet
## 3     17.2          Cojet
## 4     16.6          Cojet
## 5     19.3          Cojet
## 6     17.2       Silistor
## 7     14.3       Silistor
## 8     17.6       Silistor
## 9     21.6       Silistor
## 10    17.5       Silistor
## 11     5.5         Cimara
## 12     7.7         Cimara
## 13    12.2         Cimara
## 14    11.4         Cimara
## 15    16.4         Cimara
## 16    11.0 Ceramic Repair
## 17    12.4 Ceramic Repair
## 18    13.5 Ceramic Repair
## 19     8.9 Ceramic Repair
## 20     8.1 Ceramic Repair

Now we run a one-way ANOVA.

summary(aov(Outcome~Treatment,data=ANOVA_data))
##             Df Sum Sq Mean Sq F value  Pr(>F)   
## Treatment    3  200.0   66.66   7.545 0.00229 **
## Residuals   16  141.4    8.84                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The P-value for the one-way ANOVA is 0.00229.

Using Mosaic

Install the Mosaic package, if necessary, using the command

install.packages(“mosaic”)

Now use the aov() command from Mosaic.

library(mosaic)
summary(aov(Outcome~Treatment,data=ANOVA_data))
##             Df Sum Sq Mean Sq F value  Pr(>F)   
## Treatment    3  200.0   66.66   7.545 0.00229 **
## Residuals   16  141.4    8.84                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The P-value for the one-way ANOVA is 0.00229.