Calculate the mean by group and binding to the data set in R

32 Views Asked by At

Consider the following example:

library(dplyr)
  df <- data.frame(                              
    Cat  = c ("A","B","C","B","C","A","C","A","B"),       
    Fre = c(9,5,0,2,7,8,1,3,7))                            

I want to compute the mean grouped by the variable Cat as follows.

df_new <- df %>%
  summarise(Mean = mean(Fre))
# A tibble: 3 × 2
  Cat      Mean
  <chr>    <dbl>
1 A         6.67
2 B         4.67
3 C         2.67

Now my question is: how do I bind the calculated mean to my original data df? The output I need looks the following;

        Cat       Fre   Mean
1        A         9    6.67
2        B         5    4.67
3        C         0    2.67
4        B         2    4.67
5        C         7    2.67
6        A         8    6.67
7        C         1    2.67
8        A         3    6.67
9        B         7    4.67

Thank you in advance!

2

There are 2 best solutions below

0
Clemsang On BEST ANSWER

You can do:

df_new <- df %>%
  group_by(Cat) %>%
  mutate(Mean = mean(Fre)) %>%
  ungroup()

mutate is used instead of summarise to preserve rows

0
MM1 On

Can use a left join:

mean_df <- df %>%
  group_by(Cat) %>%
  summarise(Mean = mean(Fre))

result <- df %>%
  left_join(mean_df, by = "Cat")

print(result)