filtering intraday data R

220 Views Asked by At

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.

3

There are 3 best solutions below

1
On BEST ANSWER

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"), ]
0
On
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)
0
On

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)