I have a time series with 15 minutes intervals. I would like to change it into 1 hour interval using R. So the results of the measurements will be added together as well. Could you please help me with this? And is it possible to change it after that from hours to month?
The data frame is as below:
timestamp (UTC) value
2020-06-11 22:15:00 5,841
2020-06-11 22:30:00 5,719
2020-06-11 22:45:00 5,841
2020-06-11 23:00:00 5,841
2020-06-11 23:15:00 5,597
2020-06-11 23:30:00 5,232
2020-06-11 23:45:00 5,476
2020-06-12 0:00:00 4,259
2020-06-12 0:15:00 0,243
2020-06-12 0:30:00 0,243
2020-06-12 0:45:00 0,365
2020-06-12 1:00:00 0,243
Thanks in advance!!
CodePudding user response:
Depending on how you count, every 15 mins after an hour belongs to the next, you can use lubridate::ceiling_date
(22:15 => 23:00), if it belongs to the same hour, use lubridate::floor_date
(22:15 => 22:00).
library(dplyr)
library(lubridate)
# option 1
df1 %>%
mutate(timestamp = ceiling_date(timestamp, unit = "hour")) %>%
group_by(timestamp) %>%
summarise(value = sum(value))
# A tibble: 3 × 2
timestamp value
<dttm> <dbl>
1 2020-06-11 23:00:00 23.2
2 2020-06-12 00:00:00 20.6
3 2020-06-12 01:00:00 1.09
#option 2
df1 %>%
mutate(timestamp = floor_date(timestamp, unit = "hour")) %>%
group_by(timestamp) %>%
summarise(value = sum(value))
# A tibble: 4 × 2
timestamp value
<dttm> <dbl>
1 2020-06-11 22:00:00 17.4
2 2020-06-11 23:00:00 22.1
3 2020-06-12 00:00:00 5.11
4 2020-06-12 01:00:00 0.243
data:
df1 <- structure(list(timestamp = structure(c(1591906500, 1591907400,
1591908300, 1591909200, 1591910100, 1591911000, 1591911900, 1591912800,
1591913700, 1591914600, 1591915500, 1591916400), class = c("POSIXct",
"POSIXt"), tzone = ""), value = c(5.841, 5.719, 5.841, 5.841,
5.597, 5.232, 5.476, 4.259, 0.243, 0.243, 0.365, 0.243)), row.names = c(NA,
-12L), class = "data.frame")