r - 每天选择一天中的一小时进行分析
问题描述
拥有 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) )
解决方案
由于没有提供数据,我将尝试用示例数据回答您的问题:
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
推荐阅读
- android - API 28 (P) 的 Android 设计支持库不起作用
- r - R_Error in data[, 1] 中的 CIR 参数校准:维数不正确
- vba - VBA:使用从 excel 到 Word 的过滤行运行宏
- javascript - JS在几句话后剪切文本
- python - matplotlib中x轴上的“原点”和点“0”之间的差距?
- python - 有效地从包的许多子模块中导入名称
- apache-spark - spark ML 管道为不同的运行提供不同的值
- scala - 如何将scala中的十六进制十进制列转换为int
- python-2.7 - 股票市场上的网络抓取 - AttributeError:“NoneType”对象没有属性“文本”
- json - 节点 events.js:167 throw er; // 未处理的“错误”事件