Using Dates with the data.table package

前端 未结 1 1129
清酒与你
清酒与你 2021-02-14 01:04

I recently discovered the data.table package and was now wondering whether or not I should replace some of my plyr-code. To summarize, I really like plyr and I basically achieve

1条回答
  •  长情又很酷
    2021-02-14 01:43

    This should work:

    DT <- data.table(Date=as.ITime(rep(c(Sys.time(), Sys.time() + 60), each=6)),
                     y=c(rnorm(6, 1), rnorm(6, -1)))
    setkey(DT, Date)
    

    The data.table package contains some date/time classes with integer storage mode. See ?IDateTime:

    Date and time classes with integer storage for fast sorting and grouping. Still experimental!

    • IDate is a date class derived from Date. It has the same internal representation as the Date class, except the storage mode is integer.
    • ITime is a time-of-day class stored as the integer number of seconds in the day. as.ITime does not allow days longer than 24 hours. Because ITime is stored in seconds, you can add it to a POSIXct object, but you should not add it to a Date object.
    • IDateTime takes a date-time input and returns a data table with columns date and time.

    0 讨论(0)
提交回复
热议问题