Extracting time from dataframe

Hi everyone,

I have a question, I have a data frame with a ts column in Posixct format which includes date and time of several months. I want to extract just the data between 12:00 am till 3:00 am of all these days. I could not find a code for this purpose. I can do this step by step, first subsetting the date and then subsetting the time. But it takes too much time. Does anybody know how can I subset theses time from all days just by running one code? I also tried the xts package but it was not helpful.

Thanks

Welcome to the community!

It is indeed possible, as illustrated in the example below. If that doesn't solve your problem, can you please provide a REPRoducible EXample of your problem? It provides more specifics of your problem, and it helps others to understand what problem you are facing.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

set.seed(seed = 44059)

# https://stackoverflow.com/a/14721124
start_time <- as.POSIXct(x = "01/01/2019 00:00:00",
                         format = "%d/%m/%Y %H:%M:%S")
end_time <- as.POSIXct(x = "31/12/2019 23:59:59",
                       format = "%d/%m/%Y %H:%M:%S")
time_difference <- difftime(time1 = end_time,
                            time2 = start_time,
                            units = "secs")
random_time_differences <- runif(n = 1000,
                                 min = 0,
                                 max = as.numeric(x = time_difference))
random_times <- start_time + random_time_differences

fake_dataset <- tibble(id = seq_len(length.out = 1000),
                       fake_date_time = random_times)

fake_dataset_filtered_for_november <- filter(.data = fake_dataset,
                                             between(x = fake_date_time,
                                                     left = as.POSIXct(x = "01/11/2019 00:00:00",
                                                                       format = "%d/%m/%Y %H:%M:%S"),
                                                     right = as.POSIXct(x = "30/11/2019 23:59:59",
                                                                        format = "%d/%m/%Y %H:%M:%S")))

range(fake_dataset$fake_date_time)
#> [1] "2019-01-01 01:45:06 IST" "2019-12-31 17:04:15 IST"
range(fake_dataset_filtered_for_november$fake_date_time)
#> [1] "2019-11-01 09:10:03 IST" "2019-11-30 07:58:01 IST"

Created on 2019-11-05 by the reprex package (v0.3.0)

Hope this helps.


Note: Regarding the point FJCC raised below, I didn't understand what exactly OP wanted, as he mentioned filtering for times between 12 am to 3 am (what FJCC solved below), and he/she also mentioned of subsetting dates first and times next (not necessary for the previous point). My post above was intended just as an illustration that filtering based on date-time is indeed possible in R.

1 Like

I understand the question differently than Yarnabrina did. Here is my version.

library(lubridate)
library(dplyr)
DF <- data.frame(DateTime = seq.POSIXt(ymd_hms("2019-11-05 00:00:00"),
                                     ymd_hms("2019-11-06 23:59:59"), by = "15 min"))
head(DF, 15)
#>               DateTime
#> 1  2019-11-05 00:00:00
#> 2  2019-11-05 00:15:00
#> 3  2019-11-05 00:30:00
#> 4  2019-11-05 00:45:00
#> 5  2019-11-05 01:00:00
#> 6  2019-11-05 01:15:00
#> 7  2019-11-05 01:30:00
#> 8  2019-11-05 01:45:00
#> 9  2019-11-05 02:00:00
#> 10 2019-11-05 02:15:00
#> 11 2019-11-05 02:30:00
#> 12 2019-11-05 02:45:00
#> 13 2019-11-05 03:00:00
#> 14 2019-11-05 03:15:00
#> 15 2019-11-05 03:30:00
DF2 <- DF %>% filter(hour(DateTime) < 3)
head(DF2, 15)
#>               DateTime
#> 1  2019-11-05 00:00:00
#> 2  2019-11-05 00:15:00
#> 3  2019-11-05 00:30:00
#> 4  2019-11-05 00:45:00
#> 5  2019-11-05 01:00:00
#> 6  2019-11-05 01:15:00
#> 7  2019-11-05 01:30:00
#> 8  2019-11-05 01:45:00
#> 9  2019-11-05 02:00:00
#> 10 2019-11-05 02:15:00
#> 11 2019-11-05 02:30:00
#> 12 2019-11-05 02:45:00
#> 13 2019-11-06 00:00:00
#> 14 2019-11-06 00:15:00
#> 15 2019-11-06 00:30:00

Created on 2019-11-05 by the reprex package (v0.3.0.9000)

2 Likes