1

I am trying to calculate the hourly mean of x in a data frame similar to this:

  min hour day month year    x
1  10   22  31    12 2013 18.3
2  30   22  31    12 2013 16.5
3  50   22  31    12 2013 15.7
4  10   23  31    12 2013 16.7
5  30   23  31    12 2013 18.0
6  50   23  31    12 2013 18.1
7  10    0   1     1 2014 17.4
8  30    0   1     1 2014 15.4
9  50    0   1     1 2014 16.9

I have tried different things with the aggregate function but without luck. So what I want is to calculate the mean of x if the value of hour, day, month, year are the same. The output should be like below:

  hour day month year        x
1   22  31    12 2013 16.83333
2   23  31    12 2013 17.60000
3    0   1     1 2014 16.56667
smci
  • 32,567
  • 20
  • 113
  • 146
Christoffer
  • 643
  • 1
  • 6
  • 22

1 Answers1

3

Give this a try..

require(dplyr)

mydf %>%
   group_by(year, month, day, hour) %>%
   summarise (average = mean(x))
jenswirf
  • 7,087
  • 11
  • 45
  • 65