juansalix
juansalix

Reputation: 533

How to run one way ANOVA in R

I've a dataset looking like this:

> print(mydata)
                col1                 col2                col3
1               0.819               0.851               0.874
2               0.972               0.703               0.821
3               0.891               0.790               0.951
4               0.839               0.799               0.819

I would like to know if there are significant differences between the three groups col1, col2 and col3. For this matter, my guess is that the best way is to run an anova test.

Please find below the script I used to produce the dataset, to run the test and the Error displayed by R:


> mydata <- data.frame(col1, col2, col3)
> accuracymetrics <- as.vector(mydata)
> anova(accuracymetrics)

Error in UseMethod("anova") : no applicable method for 'anova' applied to an object of class "data.frame"

It's the first time I'm running such an analysis in R so bear with me if this question is not interesting for the forum. Any input to solve this error is appreciated!

Upvotes: 0

Views: 4052

Answers (1)

Cettt
Cettt

Reputation: 11981

if I understood you correctly the three groups you are talking about are the three columns in your data. If this is the case you need to do two things:

First, reshape your data from wide to long format such that it looks like this

group | value
------------
grp1  | 0.819
grp1  | 0.972

This can easily be done with the tidyr package

library(tidyr)
longdata <- gather(mydata, group, value)

Second: you have to use aov instead of anova:

res.aov <- aov(value ~ group, data = longdata)
summary(res.aov)

Here you can find even more details. Hope this helps.

Upvotes: 1

Related Questions