0

I have calculated the mean of each group for a particular column. Now I want this data to be inserted back into the data frame as a new column against the correct row

I was referring this article for calculating the means per group (which is success) - link

So now if I have my data like this

    Name     Month  Rate1     Rate2
    Aira       1      12        23
    Aira       2      18        73
    Aira       3      19        45
    Ben        1      53        19
    Ben        2      22        87
    Ben        3      19        45
    Cat        1      22        87
    Cat        2      67        43
    Cat        3      45        32

And the calculated means per group name like this

  Group.1    Rate1    Rate2
1    Aira 16.33333 47.00000
2     Ben 31.33333 50.33333
3     Cat 44.66667 54.00000

How can I put this new mean calculated against each correct row back into the data frame

So the desired output is like this

Name     Month  Rate1     Rate2     Avg rate1       Avg Rate2
Aira       1      12        23      16.3333         47.0000
Aira       2      18        73      16.3333         47.0000
Aira       3      19        45      16.3333         47.0000
Ben        1      53        19      31.3333         50.3333
Ben        2      22        87      31.3333         50.3333
Ben        3      19        45      31.3333         50.3333
Cat        1      22        87      44.6667         54.0000
Cat        2      67        43      44.6667         54.0000
Cat        3      45        32      44.6667         54.0000
Community
  • 1
  • 1
Hardik Gupta
  • 4,700
  • 9
  • 41
  • 83

1 Answers1

1

Using the dplyr package:

left_join(df, df_summarised, c("Name" = "Group.1"))

PaulH
  • 181
  • 1
  • 5