0

I'm trying to filter intraday-data to include only certain period inside the day. Is there a trick in some packages to achieve this. Here is example data:

library(tibbletime)

example <- as.tibble(data.frame(
  date = ymd_hms(seq(as.POSIXct("2017-01-01 09:00:00"), as.POSIXct("2017-01-02 20:00:00"), by="min")),
  value = rep(1, 2101)))

I would like to include only 10:00:00 - 18:35:00 for each day, but can't achieve this nicely. My solution for now has been creating extra indic columns and then filter by them, but it hasn't worked well either.

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
Hakki
  • 1,440
  • 12
  • 26
  • If you wanted to stick in the tidyverse you could split the datetime column into date and time using `separate(., date, into = c("date", "time"), sep = " ")` and then `filter` to only times `%in%` the range you want. – amanda Nov 07 '17 at 14:17

3 Answers3

1

You can use the function between() from data.table

example[data.table::between(format(example$date, "%H:%M:%S"), 
                            lower = "10:00:00",
                            upper = "18:35:00"), ]
clemens
  • 6,653
  • 2
  • 19
  • 31
  • thanks, it is always data.table and I have been trying to learn tidyverse. Will keep this alive for few minutes if there is tidyverse answers there. – Hakki Nov 07 '17 at 14:13
0
library(tibbletime)
library(tidyverse)
library(lubridate)

example <- as.tibble(data.frame(
  date = ymd_hms(seq(as.POSIXct("2017-01-01 09:00:00"), as.POSIXct("2017-01-02 20:00:00"), by="min")),
  value = rep(1, 2101)))

example %>%
  mutate(time = as.numeric(paste0(hour(date),".",minute(date)))) %>%
  filter(time >= 10 & time <= 18.35) %>%
  select(-time)
AntoniosK
  • 15,991
  • 2
  • 19
  • 32
0

This is pretty hacky but if you really want to stay in the tidyverse:

rng <- range((hms("10:00:00") %>% as_datetime()), (hms("18:35:00") %>% as_datetime()))

example %>% 
  separate(., date, into = c("date", "time"), sep = " ") %>% 
  mutate(
    time = hms(time) %>% as_datetime(),
    date = as_date(date)
  ) %>% 
  filter(time > rng[1] & time < rng[2]) %>% 
  separate(., time, into = c("useless", "time"), sep = " ") %>% 
  select(-useless)
amanda
  • 321
  • 5
  • 12