Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding Time count between a time interval

I have a time variable say x. The class of my vector is a character

x <- c("06:59:20","19:13:31", "06:08:02",  "00:25:14",  "19:25:01" ,"21:21:44"
         ,"19:04:25" , "19:43:31",  "16:00:13",  "05:15:37",  "05:52:16",  "21:47:51",  "06:33:36"
        ,"06:59:29" ,"20:06:04",  "05:08:15",  "20:09:51",  "21:52:25",  "19:07:22",  "19:37:26")

I convert it into a times object by using the below approach

x_posix <- as.POSIXct(x, format = "%H:%M:%S")
x<- strftime(x_posix, format="%H:%M:%S")
x <- times(x)


[1] 06:59:20 19:13:31 06:08:02 00:25:14 19:25:01 21:21:44 19:04:25 19:43:31 16:00:13 05:15:37 05:52:16 21:47:51 06:33:36 06:59:29 20:06:04
[16] 05:08:15 20:09:51 21:52:25 19:07:22 19:37:26

Now I want to find out how many values are lie between any time interval. Suppose If I want to find out that how many values are between 6 to 7 then I want to include all those values that are in between 6:00:00(including 6:00:00) and up to all values that are less than or equal to 6:59:59. Is there any way in R to accomplish this task ?

like image 230
learner Avatar asked Jun 23 '26 06:06

learner


1 Answers

We can use

library(chron)
sum(x1 >= times("06:00:00") & x1 < times("07:00:00"))
#[1] 4

data

x1 <- times(x) 
like image 144
akrun Avatar answered Jun 24 '26 21:06

akrun