Extracting time from dataframe

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