Compute monthly averages from daily data

我与影子孤独终老i 提交于 2019-11-27 23:19:09

One way, using base R would be to make sure your dates are of class Date or similar ( e.g. POSIXct) if you haven't already, and then to extract the months and years (as your data spans more than one year) and aggregate like so:

#  Convert to date if not already
df1$X1 <- as.Date(df1$X1)

#  Get months
df1$Month <- months(df1$X1)

#  Get years
df1$Year <- format(df1$X1,format="%y")

#  Aggregate 'X2' on months and year and get mean
aggregate( X2 ~ Month + Year , df1 , mean )
#    Month Year        X2
#1 December   09 0.0000000
#2 February   10 0.1714286
#3  January   10 1.2074074

There are quite a few ways of doing this if you have a look around.

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