Created
June 16, 2017 14:41
-
-
Save refik/529972bdbd64f2cc03e5eebbbd42a726 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(lubridate) | |
library(tibble) | |
library(dplyr) | |
library(tidyr) | |
t <- ymd_hms("2017/06/14 10:00:00") | |
tp1 <- ymd_hms("2017/06/15 15:00:00") | |
# 10:00 T | |
# 15:30 T + 1 | |
sample_data <- | |
tibble(datetime = c(ymd_hms("2017/06/14 00:00:00", tz = "EET"), | |
ymd_hms("2017/06/14 06:00:00", tz = "EET"), | |
ymd_hms("2017/06/14 12:00:00", tz = "EET"), | |
ymd_hms("2017/06/14 18:00:00", tz = "EET"), | |
ymd_hms("2017/06/15 00:00:00", tz = "EET"), | |
ymd_hms("2017/06/15 06:00:00", tz = "EET"), | |
ymd_hms("2017/06/15 12:00:00", tz = "EET"), | |
ymd_hms("2017/06/15 18:00:00", tz = "EET"), | |
ymd_hms("2017/06/16 00:00:00", tz = "EET"), | |
ymd_hms("2017/06/16 06:00:00", tz = "EET"), | |
ymd_hms("2017/06/16 12:00:00", tz = "EET"), | |
ymd_hms("2017/06/16 18:00:00", tz = "EET")), | |
sum_1 = rep(seq(0.1, 0.3, 0.1), 4), | |
sum_2 = rep(seq(1, 3, 1), 4), | |
sum_3 = rep(seq(10, 30, 10), 4)) | |
result_one <- sample_data %>% | |
mutate(group = if_else(hour(datetime) >= 0 & hour(datetime) < 10, | |
"a", | |
if_else(hour(datetime) >= 10 & hour(datetime) < 15, | |
"b", | |
if_else(hour(datetime) >= 15, | |
"c", | |
"unknown")))) %>% | |
mutate(date = as.Date(datetime)) %>% | |
mutate(include = if_else(group == "a", date - days(1), date)) | |
result_two <- result_one %>% | |
filter(group == "b") %>% | |
mutate(include = date - days(1)) | |
result <- bind_rows(result_one, result_two) | |
final <- result %>% | |
group_by(include) %>% | |
summarise_at(c("sum_1", "sum_2", "sum_3"), sum) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment