Subset rows according to time range (including minutes)

My question is mainly related to the question that Wet Feet previously raised here by user:

This is a modified data set:

      date_time loc_id node  energy   kgco2 
1 2009-02-27 00:11:08     87  103 0.00000 0.00000 
2 2009-02-27 01:05:05     87  103 7.00000 3.75900 
3 2009-02-27 02:05:05     87  103 6.40039 3.43701 
4 2009-02-28 02:10:05     87  103 4.79883 2.57697 
5 2009-02-28 04:05:05     87  103 4.10156 2.20254 
6 2009-02-28 05:05:05     87  103 2.59961 1.39599
7 2009-03-01 03:20:05     87  103 2.59961 1.39599

I try to get only those lines that fall within a certain period of time, for example. 02:05:00 to 03:30:00.

3 2009-02-27 02:05:05     87  103 6.40039 3.43701 
4 2009-02-28 02:10:05     87  103 4.79883 2.57697
7 2009-03-01 03:20:05     87  103 2.59961 1.39599

Applying the solution to a related question ( hourfrom the package lubridate), however, is not enough, since I also need to consider the minutes of my interval. I could use the function intervalfrom the package lubridateto include the minutes, but since my framework covers different dates, this will not help.

, - , dplyr filter. xts ?

+4
1

, , . ( UTC), .

1.

library(lubridate)
library(tidyverse)

dat = read.table(text="date_time time loc_id node  energy   kgco2 
                 1 2009-02-27 00:11:08     87  103 0.00000 0.00000 
                 2 2009-02-27 01:05:05     87  103 7.00000 3.75900 
                 3 2009-02-27 02:05:05     87  103 6.40039 3.43701 
                 4 2009-02-28 02:10:05     87  103 4.79883 2.57697 
                 5 2009-02-28 04:05:05     87  103 4.10156 2.20254 
                 6 2009-02-28 05:05:05     87  103 2.59961 1.39599
                 7 2009-03-01 03:20:05     87  103 2.59961 1.39599", 
                 header=TRUE, stringsAsFactors=FALSE)

dat$date_time = as.POSIXct(paste0(dat$date_time, dat$time), tz="UTC")
dat = dat %>% select(-time)

2. hms

hms_to_numeric = function(x) {
  x = as.POSIXct(paste("2010-01-01", x))
  3600 * hour(x) + 60 * minute(x) + second(x)
}

3.

dat %>% 
  filter(between(as.numeric(date_time) - as.numeric(as.POSIXct(substr(date_time,1,10), tz="UTC")), 
                 hms_to_numeric("02:05:00"), 
                 hms_to_numeric("03:30:00")))
            date_time loc_id node  energy   kgco2
1 2009-02-27 02:05:05     87  103 6.40039 3.43701
2 2009-02-28 02:10:05     87  103 4.79883 2.57697
3 2009-03-01 03:20:05     87  103 2.59961 1.39599
0

Source: https://habr.com/ru/post/1684166/


All Articles