Follow this tutorial if you need to get your data into the proper format for an ANOVA model.

#load in some data
#this is just example data
#let's say I want to compare the actual, average, and record temperatures from the NYC temperature data
setwd("~/Binghamton/dida130")
temps <- read.csv("temps_nyc.csv")

head(temps)

As you can see, each temperature type has its own column. Ideally, we need one temp variable and one grouping variable to put this in the formula for ANOVA, but it’s not quite there yet. Here’s how we do it:

#install the tidyr package first
library(tidyr)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#select the variables we want
#we'll keep the date variable so we know what day each temp is
#you should keep one unique column like this (probably year or state or something like that)
data <- temps %>% select(date, actual_max_temp, average_max_temp, record_max_temp)

#let's reshape the data so that we only have two column instead of three, which won't work well with the ANOVA function
#change column names to make them nicer
colnames(data) <- c("date", "actual", "average", "record")

#let's disect this code: First, we aren't changing the id (date) column - leave this out using a !
#next, we specify our grouping variable name, temperature_type
#then, we specify the name of the column with our values/numbers in it, temperatures
data <- data %>% 
  pivot_longer(!date, names_to = "Temperature_type", values_to = "Temperatures")

head(data)
#now, I can perform an ANOVA on this!
my_aov <- aov(Temperatures~Temperature_type, data=data)
summary(my_aov)
##                    Df Sum Sq Mean Sq F value Pr(>F)    
## Temperature_type    2 115925   57963   213.5 <2e-16 ***
## Residuals        1092 296532     272                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1