Home > database >  Is there an R code I could use to average the row values that have the same Date AND Code?
Is there an R code I could use to average the row values that have the same Date AND Code?

Time:05-27

I would like to go from this: | Date | Code| Value| |--------| ----|------| |2017-S1 | 168 | 20000| |2017-S1 | 168 | 25000| |2017-S2 | 168 | 22000| |2017-S1 | 169 | 20000| |2017-S2 | 169 | 26000|

To this: | Date | Code| Value| |--------| ----|------| |2017-S1 | 168 | 22500| |2017-S2 | 168 | 22000| |2017-S1 | 169 | 20000| |2017-S2 | 169 | 26000|

CodePudding user response:

You need to group_by the variables Code and Date and then summarise Value using the meanfunction:

library(dplyr)
df %>%
   group_by(Date, Code) %>%
   summarise(Value = mean(Value))
  •  Tags:  
  • r
  • Related