简体   繁体   中英

Categorize time intervals to dates in R

I have a very large dataset where I need to split time intervals into dates for further analysis.

Below an example of my dataset:

require(data.table)

RawDT = data.table(
   TimeStampID = c("4"),
  DateTimeFrom = c("2019-02-10 16:28:03"),
    DateTimeTo = c("2019-02-12 02:04:03")
)

Below is the desired result:

ResultDT = data.table(
           ID = c("1","2","3"),
  TimeStampID = c("4","4","4"),
           DS = c("2019-02-10","2019-02-11","2019-02-12"),
     TimeFrom = c("16:28:03","00:00:00","00:00:00"),
       TimeTo = c("23:59:59","23:59:59","02:04:03")
)

Can anyone guide me to which function to use to achieve ResultDT from RawDT?

OK, this is borderline duplicate - so I encourage the moderators to close the topic if they consider it appropriate to do so, and I'll delete my post.

However, I had a similar (but not exactly identical, that's why I'm answering) problem with beginning and end of year ( here ), and @Jaap created a great (and succinct!) solution the logic of which can be applied also here, for instance:

library(data.table)

RawDT[, `:=` (DateTimeFrom = as.POSIXct(DateTimeFrom), DateTimeTo = as.POSIXct(DateTimeTo))]

RawDT[RawDT[, rep(.I, 1 + as.Date(DateTimeTo) - as.Date(DateTimeFrom))]
   ][, `:=` (DateTimeFrom = pmax(DateTimeFrom[1], as.POSIXct(paste0(as.Date(DateTimeFrom[1]) + 0:(.N-1), ' 00:00:00'))),
             DateTimeTo = pmin(DateTimeTo[.N], as.POSIXct(paste0(as.Date(DateTimeTo[.N]) - (.N-1):0, ' 23:59:59'))))
     , by = .(TimeStampID, rleid(DateTimeFrom))][]

I've added an additional group to your DT just to test the functionality:

RawDT = data.table(
  TimeStampID = c("4", "5"),
  DateTimeFrom = c("2019-02-10 16:28:03", "2019-03-15 12:28:03"),
  DateTimeTo = c("2019-02-12 02:04:03", "2019-03-20 14:45:00")
)

And the output for the above code would be:

   TimeStampID        DateTimeFrom          DateTimeTo
1:           4 2019-02-10 16:28:03 2019-02-10 23:59:59
2:           4 2019-02-11 00:00:00 2019-02-11 23:59:59
3:           4 2019-02-12 00:00:00 2019-02-12 02:04:03
4:           5 2019-03-15 12:28:03 2019-03-15 23:59:59
5:           5 2019-03-16 00:00:00 2019-03-16 23:59:59
6:           5 2019-03-17 00:00:00 2019-03-17 23:59:59
7:           5 2019-03-18 00:00:00 2019-03-18 23:59:59
8:           5 2019-03-19 00:00:00 2019-03-19 23:59:59
9:           5 2019-03-20 00:00:00 2019-03-20 14:45:00

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