如何在R中将给定的时间序列数据集划分为4小时窗口 [英] How to divide a given time series dataset into 4 hour window in R

查看:26
本文介绍了如何在R中将给定的时间序列数据集划分为4小时窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于给定的一天,我有一个类似这样的时间序列数据帧.

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)

我想将此数据帧子集化为4小时窗口并绘制它们,因此我得到6个图(12 AM-4AM、4AM-8AM、8AM-12PM、12PM-4PM、4PM-8PM、8PM-12AM).

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).

我正在通过这种方式使用data.table将其分为12小时窗口(上午和下午)

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)] ) ]

我想做类似的事情,但是有一个4小时的窗口,并且还子集了数据帧(6个数据帧).

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

推荐答案

这是主要使用 cut 并通过 lubridate :: hour提取 hour 的一种方法:

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)

这篇关于如何在R中将给定的时间序列数据集划分为4小时窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆