ID cat1 cat2 cat3 loss
1 A B D 2213.18
2 A B A 1283.60
3 A B B 3005.09
4 B A A 939.85
5 A B C 2763.85
6 A A A 5142.87
There are 116 categorical variables of different levels of which I have listed down three. Below is the function I have used to calculate mean(loss) for every level in a variable
a1<-summarise(group_by(ins,cat85), cat85_mean=mean(loss))
Need a code which dynamically does this for the remaining variables so that I have the mean(loss) for all the categorical variables across different levels
Eg : Cat85 has 4 levels namely A,B,C and D. The function should generate the mean(loss) for A, B, C and D like A-2000, B-1234.5, C-5667.5, D- 3465.2.
Thanks!