I have a data table with one of the columns as date time
data = data.table::data.table(date_time = c("2019-05-20 14:20:00","2019-05-20 14:21:00","2019-05-20 14:22:00","2019-05-20 14:23:00","2019-05-20 14:24:00",
"2019-05-21 14:20:00","2019-05-20 14:21:00","2019-05-20 14:22:00","2019-05-20 14:23:00","2019-05-20 14:24:00",
"2019-05-20 14:20:00","2019-05-20 14:21:00","2019-05-20 14:22:00","2019-05-20 14:23:00","2019-05-20 14:24:00") )
data[ , date_time := as.POSIXct(date_time, "%Y-%m-%d %H:%m:%s") ]
I want to remove all rows which contain time between 14:21:00 and 14:23:00 for each date.
I tried solutions in R Filtering Time Series data for each group and R Filtering data using reference time table in R but they are not replicable for each date.
CodePudding user response:
With lubridate
, using hms
periods.
This approach allows to have different start & end hours:
library(data.table)
library(lubridate)
data[ , date_time := as.POSIXct(date_time, format="%Y-%m-%d %H:%M:%S") ]
data[!between(date_time,floor_date(date_time,'day') hms('14:21:00'),
floor_date(date_time,'day') hms('14:23:00'))]
# date_time
# <POSc>
#1: 2019-05-20 14:20:00
#2: 2019-05-20 14:24:00
#3: 2019-05-21 14:20:00
#4: 2019-05-20 14:24:00
#5: 2019-05-20 14:20:00
#6: 2019-05-20 14:24:00
CodePudding user response:
Since you are already using data.table
, it has convenient functions hour
and minute
which you can use here.
library(data.table)
data[ , date_time := as.POSIXct(date_time, format = "%Y-%m-%d %T")]
data[!(hour(date_time) == 14 & between(minute(date_time), 21, 23))]
# date_time
#1: 2019-05-20 14:20:00
#2: 2019-05-20 14:24:00
#3: 2019-05-21 14:20:00
#4: 2019-05-20 14:24:00
#5: 2019-05-20 14:20:00
#6: 2019-05-20 14:24:00