김창한 김창한 - 3 months ago 9
R Question

I want to make a dataset except specific time period

my dataset have time column

time
2016-07-29 05:24:00
2016-07-29 05:24:00
2016-07-29 03:56:00
2016-07-29 03:56:00
2016-07-29 03:48:00
2016-07-29 03:48:00
2016-07-29 03:44:00
2016-07-29 03:44:00
2016-07-29 23:00:00
2016-07-29 23:00:00
2016-07-29 23:00:00
2016-07-29 23:00:00
2016-07-29 23:00:00
2016-07-29 23:00:00
2016-07-30 04:15:00
2016-07-30 04:15:00
2016-07-30 04:15:00
2016-07-30 04:15:00
2016-07-30 14:15:00
2016-07-30 14:15:00
2016-07-30 14:15:00
2016-07-30 14:15:00
...


I want to make a dataset except specific time period
If I choose row data except 00:00 ~ 06:00

time
2016-07-29 23:00:00
2016-07-29 23:00:00
2016-07-29 23:00:00
2016-07-29 23:00:00
2016-07-29 23:00:00
2016-07-29 23:00:00
2016-07-30 14:15:00
2016-07-30 14:15:00
2016-07-30 14:15:00
2016-07-30 14:15:00
...

Answer

We extract the 'time' part with format, then convert to POSIXct (to have the same date), then create another vector of datetime for the '06:00' time, use the logical operator (>) and subset the rows.

v1 <- as.POSIXct(format(as.POSIXct(df1$time), "%H:%M"), format = "%H:%M")
s2 <- as.POSIXct("06:00", format = "%H:%M")
df1[v1 > s2, , drop=FALSE]
#                 time
#9  2016-07-29 23:00:00
#10 2016-07-29 23:00:00
#11 2016-07-29 23:00:00
#12 2016-07-29 23:00:00
#13 2016-07-29 23:00:00
#14 2016-07-29 23:00:00
#19 2016-07-30 14:15:00
#20 2016-07-30 14:15:00
#21 2016-07-30 14:15:00
#22 2016-07-30 14:15:00

Or another base R option is

df1[as.numeric(gsub(".*\\s+|:.*", "", df1$time)) > 6,, drop= FALSE]

Or use strptime, extract the hour and do the logical condition

df1[strptime(df1$time, format = "%Y-%m-%d %H:%M:%S")$hour,]

NOTE: We did not use any external packages.


Or we can use data.table

library(data.table)
setDT(df1)[hour(time)>6]
#                   time
# 1: 2016-07-29 23:00:00
# 2: 2016-07-29 23:00:00
# 3: 2016-07-29 23:00:00
# 4: 2016-07-29 23:00:00
# 5: 2016-07-29 23:00:00
# 6: 2016-07-29 23:00:00
# 7: 2016-07-30 14:15:00
# 8: 2016-07-30 14:15:00
# 9: 2016-07-30 14:15:00
#10: 2016-07-30 14:15:00

data

 df1 <- structure(list(time = c("2016-07-29 05:24:00", "2016-07-29 05:24:00", 
 "2016-07-29 03:56:00", "2016-07-29 03:56:00", "2016-07-29 03:48:00", 
 "2016-07-29 03:48:00", "2016-07-29 03:44:00", "2016-07-29 03:44:00", 
 "2016-07-29 23:00:00", "2016-07-29 23:00:00", "2016-07-29 23:00:00", 
 "2016-07-29 23:00:00", "2016-07-29 23:00:00", "2016-07-29 23:00:00", 
 "2016-07-30 04:15:00", "2016-07-30 04:15:00", "2016-07-30 04:15:00", 
 "2016-07-30 04:15:00", "2016-07-30 14:15:00", "2016-07-30 14:15:00", 
 "2016-07-30 14:15:00", "2016-07-30 14:15:00")), .Names = "time", 
 class = "data.frame", row.names = c(NA, -22L))
Comments