1

I have a data frame like below (I named it height_df)

[No]   [Name]    [Height]    [Group]
 1      aaa        170.8       1
 2      aab        171.8       1
 3      aac        177.1       2
 4      aad        175.5       2
 5      aae        170.3       1
 6      aaf        172.4       1
 7      aag        180.1       3
 8      aah        169.4       1
 9      aai        181.8       3
 10     aaj        170.1       1
 11     aak        183.3       3
 12     aal        169.8       1

height_df <- data.frame(
      Name = c("aaa", "aab", "aac", "aad", "aae", "aaf", "aag", "aah", "aai", "aaj", "aak", "aal"),
      Height = c(170.8,171.8,177.1,175.5,170.3,172.4,180.1,169.4,181.8,170.1,183.3,169.8), 
      Group = c(1,1,2,2,1,1,3,1,3,1,3,1))

I want to create a data frame as below.

[No]   [Name]    [Height]    [Group]   [Avg_Height]
 1      aaa        170.8       1
 2      aab        171.8       1
 3      aac        177.1       2
 4      aad        175.5       2
 5      aae        170.3       1
 6      aaf        172.4       1
 7      aag        180.1       3
 8      aah        169.4       1
 9      aai        181.8       3
 10     aaj        170.1       1
 11     aak        183.3       3
 12     aal        169.8       1

Following are the steps that I used. I first found the average height for each group with 'dplyr'.

grouped_heights <- height_df %>% group_by(Group) %>% summarize(avgHeight = mean(Height))

Then I reassigned the average to each height_df data frame with the following code

join <- merge(height_df, grouped_heights, by.x=3, by.y=2)

I want to know whether there's a way to do it at once without having to create a temporary data frame (in this case I created grouped_heights data frame temporarily).

Mad
  • 435
  • 2
  • 17

1 Answers1

1

Instead of summarise/merge, use mutate to create a new column in the original data

library(dplyr)
height_df <- height_df %>% 
   group_by(Group) %>%
   mutate(avgHeight = mean(Height, na.rm = TRUE)) %>%
   ungroup
akrun
  • 874,273
  • 37
  • 540
  • 662