-1

I am new to R and I need help in this. I have 3 data sets from 3 different years. they have the same columns with different values for each year. I want to find the average for the column values across the three years based on the name field. To be specific:

assume : first data set

Name Age Height Weight
A     4    20     20
B     5    22     22
C     8    25     21
D     10   25     23

second data set

Name Age Height Weight
A     5    22     25
B     6    23     26

Third data set

Name Age Height Weight
A     6    24     24
B     7    24     27
C     10   27     28

I want to find the average height for "A" across the three data sets

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
S. Osman
  • 1
  • 2

1 Answers1

0

We can place them in a list and rbind them, group by 'Name' and get the mean of each column

library(data.table)
rbindlist(list(df1, df2, df3))[, lapply(.SD, mean), by = Name]

Or with dplyr

bind_rows(df1, df2, df3) %>%
          group_by(Name) %>%   
          summarise_each(funs(mean))
akrun
  • 874,273
  • 37
  • 540
  • 662