I want to apply the aggregate
function (or something with the same effect) to average say the value of Column_B
, based on the groups in Column_A
. However, in practice there will be a large amount of groups in A, and for smaller groups (say n < 30) I'm often not as interested in the result. Therefore I'd like to include the group size in a column next to it, and also possibly filter by group size.
So say I have an example table such as:
x =
Column_A Column_B
Person1 4
Person1 6
Person1 7
Person2 8
Person2 11
Person2 10
Person2 13
Person2 15
Person3 19
My desired output would be
Column_A Column_B Avg. n
Person1 5.66 3
Person2 11.4 5
Person3 19 1
I know the first two columns can be achieved using:
aggregate(x[, 2], list(x$Column_A), mean)
Similarly, I could count the number of times each person appears with a separate command, but just wondering is there any easier way of combining these together? I thought maybe adding sum
as another parameter after mean
in the function, but couldn't find an example of that.