Aggregating hourly data into daily aggregates with missing value in R

╄→尐↘猪︶ㄣ 提交于 2020-01-07 07:47:09

问题


[enter image description here][1][enter image description here][2]I have a data frame "RH", with hourly data and I want to convert it to daily maximum and minimum data. This code was very useful [question]:Aggregating hourly data into daily aggregates

RH$Date <- strptime(RH$Date,format="%y/%m/%d)
RH$day <- trunc(RH$Date,"day")

require(plyr)

x <- ddply(RH,.(Date),
  summarize,
  aveRH=mean(RH),
  maxRH=max(RH),
  minRH=min(RH)
)

But my first 5 years data are 3 hours data not hourly. so no results for those years. Any suggestion? Thank you in advance.

'data.frame': 201600 obs. of 3 variables: $ Date: chr "1985/01/01" "1985/01/01" "1985/01/01" "1985/01/01" ... $ Hour: int 1 2 3 4 5 6 7 8 9 10 ... $ RH : int NA NA 93 NA NA NA NA NA 79 NA ...


回答1:


The link you provided is an old one. The code is still perfectly good and would work, but here's a more modern version using dplyr and lubridate

df <- read.table(text='date_time value
"01/01/2000 01:00" 30
"01/01/2000 02:00" 31
"01/01/2000 03:00" 33
"12/31/2000 23:00" 25',header=TRUE,stringsAsFactors=FALSE)

library(dplyr);library(lubridate)
df %>%
  mutate(date_time=as.POSIXct(date_time,format="%m/%d/%Y %H:%M")) %>%
  group_by(date(date_time)) %>%
  summarise(mean=mean(value,na.rm=TRUE),max=max(value,na.rm=TRUE),
            min=min(value,na.rm=TRUE))

  `date(date_time)`     mean   max   min
             <date>    <dbl> <dbl> <dbl>
1        2000-01-01 31.33333    33    30
2        2000-12-31 25.00000    25    25

EDIT Since there's already a date column, this should work:

RH %>% 
 group_by(Date) %>% 
 summarise(mean=mean(RH,na.rm=TRUE),max=max(RH,na.rm=TRUE), 
           min=min(RH,na.rm=TRUE))


来源:https://stackoverflow.com/questions/44876335/aggregating-hourly-data-into-daily-aggregates-with-missing-value-in-r

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