I have data which looks like the following
nums r a
1 3 210 0
4 6 3891 1
9 8 891 1
1 3 321 1
8 1 32 0
etc etc etc
I'd like to compute a few things, and was wondering if anyone can help provide code for the following
- The mean
a
value for eachnums
value (e.g., above, the averagea
for1 3
is0.5
), sorted by the highest averagea
value. I'm thinkingtapply
would solve this, but I don't know how to handle the sort component. - The mean
a
value for eachnums
value, sorted by a predeterminednums
order. E.g., something liketapply(df$ac, df$nums, mean, orderBy=c("1 3", "4 6", "8 1", etc.))
. You can assume I have an ordering that covers every possiblenums
value.