dplyr – Calculating average value by grouping by multiple variables using Group_by function in R studio

I figured it out! I was writing my code as:

mean_valueA=df%>%
group_by(df$site, df$species)
summarize(mean_val=mean(df$ValueA)

because I thought that was the same as writing

mean_ValueA=df%>%
group_by(site, species)
summarize(mean_val=mean(valueA)

but it’s not. By referencing my dataframe everytime, I was creating these issues. I should just have referenced the column by themselves.

This post helped me a lot:Summarising giving same value for each group

Read more here: Source link