Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - Compute a summary column [duplicate]

Tags:

r

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 aggregate first (to create a temporary dataframe) and than merge 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!

like image 752
fzara Avatar asked Jun 04 '26 09:06

fzara


1 Answers

In base R, this can be easily done with ave

df$Mean <- with(df, ave(Value, Group))
df
#   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
like image 94
akrun Avatar answered Jun 07 '26 22:06

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!