简体   繁体   中英

Analysing data with time in R

I want to the frequency of observations "after 19:00" independently of date. What would be the quickest and most logical way?

As I told R that the Date column is a date as.Date, I would like to tell R that Time is a time column... and then just ask "Time > "19:00:00"" but this does not seem to be possible.

I tried as.POSIXct(Time, format= "%H:%M:%S") but this function adds a date of today to my column which creates annoying clutter and unprofessional look.

I could use substr(as.character(Time),1,2) > 19 but that doesn't feel very elegant either.

  Date     Time    
1 2014-01-01 17:16:48  
2 2014-01-01 18:57:36   
3 2014-01-01 19:40:48  
4 2014-01-01 19:40:48 
5 2014-01-01 20:09:36 
6 2014-01-01 20:24:00   
library(data.table)

## Convert (by reference) your data to a data.table
setDT(dat)
dat[, .N
    , by = list(above_1900 = hour(as.POSIXlt(Time, format="%H:%M:%S")) > 19)]

   above_19 N
1:    FALSE 4
2:     TRUE 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM