fzara fzara - 1 year ago 81
R Question

R - Compute a summary column

I am trying to compute an additional column in my dataframe that contains some summary data (mean, min, max). Starting from this dataframe

Group Value
A 15
A 5
B 4
B 2
C 25
C 15

I would like to calculate means for every group:

Group Mean
A 10
B 3
C 20

But i would like to add a column to the original dataframe repeating the value for every row of the same group, like this:

Group Value Mean
A 15 10
A 5 10
B 4 3
B 2 3
C 25 20
C 15 20

I managed to obtain this result using
first (to create a temporary dataframe) and than
the original dataframe with the temporary one using "Group" as merging variable.

I am sure there is an easier and faster way to do this. Of note, i would like to be able to do this with the base functions (e.g. no dplyr, reshape, etc) if possible. Thank you!

Answer Source

In base R, this can be easily done with ave

df$Mean <- with(df, ave(Value, Group))
#   Group Value Mean
#1     A    15   10
#2     A     5   10
#3     B     4    3
#4     B     2    3
#5     C    25   20
#6     C    15   20
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download