Specific datetime range

Hi, I have problems selecting a specific DateTime range. For example, I would like to filter the datetime from 14/04/2017 17:30 to 15/05/2017 02:00.

Anyone can help me with this? I have struggled to use different codes from other websites. But it seems appear only the date without the time.

Cheers,
MHA

datetime swc_mn
14/04/2017 16:30 0.517583334
14/04/2017 17:00 0.517333333
14/04/2017 17:30 0.51725
14/04/2017 18:00 0.51725
14/04/2017 18:30 0.517016667
14/04/2017 19:00 0.517
14/04/2017 19:30 0.517
14/04/2017 20:00 0.517
14/04/2017 20:30 0.517
14/04/2017 21:00 0.517916667
14/04/2017 21:30 0.518966667
14/04/2017 22:00 0.519
14/04/2017 22:30 0.518916667
14/04/2017 23:00 0.5185
14/04/2017 23:30 0.518216667
15/04/2017 00:00 0.517883334
15/04/2017 00:30 0.519283334
15/04/2017 01:00 0.519483334
15/04/2017 01:30 0.519416667
15/04/2017 02:00 0.518616667
15/04/2017 02:30 0.518516667
15/04/2017 03:00 0.518716667

One slightly old-fashioned way with your data in a data.frame or tibble called dat1

library(lubridate)


# if needed convert the datetime column to a time
dat1[, 1]  <-  dmy_hm(dat1[, 1])

 subset(dat1, datetime >= dmy_hm("14/04/2017 17:30") & datetime <= dmy_hm("15/05/2017 02:00"))
        

subset(dat1, datetime >= dmy_hm("14/04/2017 17:30") & datetime <= dmy_hm("15/05/2017 02:00"))

suppressPackageStartupMessages({
  library(lubridate)
})

readr::read_csv("~/Desktop/grist.csv") -> input
#> 
#> ── Column specification ────────────────────────────────────────────────────────
#> cols(
#>   dated = col_character(),
#>   swc_mn = col_double()
#> )

spare <- input

input
#> # A tibble: 22 x 2
#>    dated            swc_mn
#>    <chr>             <dbl>
#>  1 14/04/2017 16:30  0.518
#>  2 14/04/2017 17:00  0.517
#>  3 14/04/2017 17:30  0.517
#>  4 14/04/2017 18:00  0.517
#>  5 14/04/2017 18:30  0.517
#>  6 14/04/2017 19:00  0.517
#>  7 14/04/2017 19:30  0.517
#>  8 14/04/2017 20:00  0.517
#>  9 14/04/2017 20:30  0.517
#> 10 14/04/2017 21:00  0.518
#> # … with 12 more rows

# convert dated to dttm object

input[,1] <- dmy_hm(input[,1][[1]])

# selection criteria

begin <- input[3,1][[1]]
finish   <- input[5,1][[1]]

# define interval object

span <- interval(begin,finish)

# use interval object to find inclusive range

locs <- which(input[,1][[1]] %within% span)

# show result

input[locs,]
#> # A tibble: 3 x 2
#>   dated               swc_mn
#>   <dttm>               <dbl>
#> 1 2017-04-14 17:30:00  0.517
#> 2 2017-04-14 18:00:00  0.517
#> 3 2017-04-14 18:30:00  0.517

Hi,

The output was below:

subset(df, datetime >= dmy_hm("2018-08-31 00:00") & datetime <= dmy_hm("2018-09-07 23:30"))

Warning messages:
1: All formats failed to parse. No formats found.
2: In eval(e, x, parent.frame()) :
Incompatible methods ("Ops.factor", ">=.POSIXt") for ">="
3: All formats failed to parse. No formats found.
4: In eval(e, x, parent.frame()) :
Incompatible methods ("Ops.factor", "<=.POSIXt") for "<="

Use ymd_hm for datetime strings already in ISO format. dmy_hm only works "15/1/2021 14:30"