user6678274 user6678274 - 3 months ago 13
R Question

How to subset times in R

In R I have data

USER BIRTH
11 "2013-01-11 22:31:11"
121 "2014-12-26 04:07:35"
...


I want to create a new data set
data_new
that contain all
USER
in the time 10 o'clock to 11 o'clock.
The types of
USER
and
BIRTH
are strings/characters. I tried this:

data_new= data$BIRTH > as.POSIXct("10:00:00", format="%H:%M:%S")
& data$BIRTH < as.POSIXct("11:00:00", format="%H:%M:%S")


but here R gives we FALSE for all entries, so this don't work.
How can I solve this?

Update

Say I want to find the number of users for all hours. I use the answer and try this

u=c()
for(j in 1:24) {
data_new=data[times > "00:00:00"+(j-1) & times < "01:00:00"+j ,]
#saving the number of users in vector u
u[j]=dim(data_new)[1]
}


but R can't figure out the term
"00:00:00"+(j-1)
.

Answer

If df is your data frame:

df <- read.table(text = 'USER   BIRTH
11     "2013-01-11 22:31:11"
121    "2014-12-26 04:07:35"
121    "2014-12-26 10:07:35"
121    "2014-12-26 11:07:35"
121    "2014-12-26 10:38:35"', header = T)

df$BIRTH <- ymd_hms(df$BIRTH)

times <- strftime(df$BIRTH, format = "%H:%M:%S")
df[times > "10:00:00" & times < "11:00:00",]

Output:

  USER               BIRTH
3  121 2014-12-26 10:07:35
5  121 2014-12-26 10:38:35
Comments