首页 > 解决方案 > 每天选择一天中的一小时进行分析

问题描述

拥有 1 年每小时记录的数据集

为了分析,我需要分别提取一年中的每个月,一天中的每个小时,所以 1 月 00 日、1 月 01 日、1 月 02 日、1 月 03 日、....、...、3 月 21 日、3 月 22 日、3 月23小时

提前感谢您提供任何有用的帮助!

在具有日期时间、过滤器、子集...的时间段内选择一天中指定时间的观察值

下面的代码(过滤器,月份(时间)生成致命错误错误:“at<-subset(groenenborgerno,时间戳=小时(时间)== 01))中的意外')'

groenenborgerno$timestamp <- as.POSIXct(groenenborgerno$date, format="%Y-%m-%d %H:%M:%S")
library(lubridate)

january01<-filter(atimeframe, 
       (month(time) == 01 & hour(time) == 01) )

标签: rdatedatetime

解决方案


由于没有提供数据,我将尝试用示例数据回答您的问题:

require(lubridate)
require(tidyverse)

## Create some sample data: 
time_index <- seq(from = as.POSIXct("2017-01-01 07:00"), 
                  to = as.POSIXct("2018-01-01 18:00"), by = "hour")
value <- rnorm(n = length(time_index))
data <- data.frame(time_index,value)


data <- data %>% mutate (hour = hour(time_index),
                         month = month(time_index)) %>%
  group_by(month,hour) 

head(data)
> data
# A tibble: 8,772 x 4
# Groups:   month, hour [288]
   time_index           value  hour month
   <dttm>               <dbl> <int> <dbl>
 1 2017-01-01 07:00:00 -0.626     7     1
 2 2017-01-01 08:00:00  0.184     8     1
 3 2017-01-01 09:00:00 -0.836     9     1
 4 2017-01-01 10:00:00  1.60     10     1
 5 2017-01-01 11:00:00  0.330    11     1
 6 2017-01-01 12:00:00 -0.820    12     1
 7 2017-01-01 13:00:00  0.487    13     1
 8 2017-01-01 14:00:00  0.738    14     1
 9 2017-01-01 15:00:00  0.576    15     1
10 2017-01-01 16:00:00 -0.305    16     1
# ... with 8,762 more rows

然后就是filter()你想要的小时/月组合:

data %>% filter(hour > 12 & month == 1)

# A tibble: 347 x 4
# Groups:   month, hour [11]
   time_index            value  hour month
   <dttm>                <dbl> <int> <dbl>
 1 2017-01-01 13:00:00  0.487     13     1
 2 2017-01-01 14:00:00  0.738     14     1
 3 2017-01-01 15:00:00  0.576     15     1
 4 2017-01-01 16:00:00 -0.305     16     1
 5 2017-01-01 17:00:00  1.51      17     1
 6 2017-01-01 18:00:00  0.390     18     1
 7 2017-01-01 19:00:00 -0.621     19     1
 8 2017-01-01 20:00:00 -2.21      20     1
 9 2017-01-01 21:00:00  1.12      21     1
10 2017-01-01 22:00:00 -0.0449    22     1
# ... with 337 more rows

推荐阅读