Aggregate hourly means based on compound field expression

扶醉桌前 提交于 2020-01-06 16:30:15

问题


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

回答1:


Give this a try..

require(dplyr)

mydf %>%
   group_by(year, month, day, hour) %>%
   summarise (average = mean(x))


来源:https://stackoverflow.com/questions/24573485/aggregate-hourly-means-based-on-compound-field-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!