I have a dataframe that looks like this:
name val type group
1 A3330 184 ave IX13
2 A3330 42 min IX13
3 A1473 195 ave IX01
4 A1473 93 min IX01
5 A3330 80 ave BG12
6 A3330 44 min BG12
7 A1473 15 ave IX3
8 A1473 91 min IX3
I want to average every value in val, across groups if the type is the same. So 1 and 5 are averaged despite the fact that they are different groups. The same goes with 2 and 6, 3 and 7, and 4 and 8. In the end, I hope to have a new dataframe that looks like this:
name val type
A3330 132 ave
A333 43 min
A1473 105 ave
A1473 92 min
I am thinking that I should do something like this:
for (metric in type)
for(names in name)
mean(df[df$name==names && type==metric, df$type])
}
}
But in my limited R experience, there is always a better way than to loop. Any suggestions?