简体   繁体   中英

filtering intraday data R

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.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM