How to divide a given time series dataset into 4 hour window in R

房东的猫 提交于 2021-02-08 08:21:01

问题


I have a time series dataframe like this for a given day.

Datetime <- c("2015-09-29 00:00:13", "2015-09-29 00:45:00", "2015-09-29 02:53:20", "2015-09-29 03:22:18", 
              "2015-09-29 05:42:10", "2015-09-29 05:55:50", "2015-09-29 06:14:10", "2015-09-29 07:42:16",
              "2015-09-29 08:31:15", "2015-09-29 09:13:10", "2015-09-29 11:45:14", "2015-09-29 11:56:00", 
              "2015-09-29 13:44:00", "2015-09-29 14:41:20", "2015-09-29 15:33:10", "2015-09-29 15:24:00",
              "2015-09-29 17:24:12", "2015-09-29 17:28:16", "2015-09-29 18:22:34",  
              "2015-09-29 21:34:31", "2015-09-29 22:48:20", "2015-09-29 22:22:22", "2015-09-29 23:38:22")
Measurement <- c(0.6,0.4,0.2,0.5,0.8,0.6,0.7,0.3,0.8,0.8,
                 0.2,0.8,0.2,0.35,0.8,0.4,0.4,0.6,0.1,0.9,
                 0.6,0.2,0.2)

df1 <- data.frame(Datetime,Measurement)

I would like to subset this data frame into 4 hour window and plot them and so I get 6 plots (12AM-4AM, 4AM-8AM, 8AM-12PM, 12PM-4PM, 4PM-8PM, 8PM-12AM).

I am doing this way to subset it into 12 hour window (AM & PM) using data.table

setDT(df1)
df1[, `:=`( datetime = as.IDate(Datetime), ante_post = c("AM","PM")[1+(hour(Datetime) >= 12)] ) ]

I would like to do a similar thing but with a 4 hour window and also subset the dataframe (6 dataframes).


回答1:


Here's one approach that primarily uses cut and extracts the hour via lubridate::hour:

library(lubridate)
library(ggplot2)

df1$Datetime <- as.POSIXct(df1$Datetime)

labels_four_hr <- c("12AM - 4AM", "4AM - 8AM", "8AM - 12PM", "12PM - 4PM", "4PM - 8PM", "8PM - 12AM")
labels_six_hr  <- c("12AM - 6AM", "6AM - 12PM", "12PM - 6PM", "6PM = 12AM")

df <- df1 %>%
  mutate(hour = hour(Datetime),
         seg_four_hr = cut(hour, breaks = 0:6 / 6 * 24, include.lowest = TRUE, labels = labels_four_hr),
          seg_six_hr = cut(hour, breaks = 0:4 / 4 * 24, include.lowest = TRUE, labels = labels_six_hr))


ggplot(df, aes(x = Datetime, y = Measurement)) +
  geom_point() +
  facet_wrap(~ seg_four_hr)

ggplot(df, aes(x = Datetime, y = Measurement)) +
  geom_point() +
  facet_wrap(~ seg_six_hr)



回答2:


hour(Datetime) gives you an integer number (0-23), corresponding to the hour of that event. You can easily subset data table into 4 hour windows by using integer divisiom (%/% operator) and plot everything with ggplot2:

library(ggplot2)
df1[,group:=1+hour(Datetime)%/%4]
qplot(data=df1,x=Datetime,y=Measurement,facets=group~.)


来源:https://stackoverflow.com/questions/33480503/how-to-divide-a-given-time-series-dataset-into-4-hour-window-in-r

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