If I have a data frame that looks like
dat<-data.frame(val= c(1,2,3,4,5,6,7),category= c("A","B","c","A","B","c","D"))
dat
val category
1 1 A
2 2 B
3 3 c
4 4 A
5 5 B
6 6 c
7 7 D
I'd like to AVERAGE by the category so the output looks like
A 2.5
B 3.5
C 4.5
D 7
What's the best way to do this?
The most straightforward way would be to use
tapply
as follows:Note that if you have missing values you'd want to amend it to ignore those in the calculation of the mean
see ?tapply