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 ?
We can use
library(chron)
sum(x1 >= times("06:00:00") & x1 < times("07:00:00"))
#[1] 4
x1 <- times(x)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With