Search code examples
rdatetimetimestampposixct

Create custom time intervals for plotting time series in r


I have Timestamp data in the form: "YYYY-mm-dd HH:MM:SS". I want to be able to create custom bins of 6 hours each for plotting. How do i create these intervals in r? Please see sample data below:

2021-05-25 14:56:45.19,
2021-05-25 14:56:47.50,
2021-05-25 14:56:48.28,
2021-05-25 14:56:49.42,
2021-05-25 14:57:01.64,
2021-05-25 14:57:19.46,
2021-05-25 14:57:21.87,
2021-05-25 14:57:22.65,
2021-05-25 14:57:23.79,
2021-05-25 15:04:46.01,
2021-05-25 15:04:46.38,
2021-05-25 15:04:51.21,
2021-05-25 15:04:51.58,
2021-05-25 14:57:53.83,
2021-05-25 14:57:57.40,
2021-05-25 14:57:58.17,
2021-05-25 14:57:59.31,
2021-05-25 14:58:01.65,
2021-05-25 14:58:29.34,
2021-05-25 14:58:31.66,
2021-05-25 14:58:32.17,
2021-05-25 14:58:32.76,
2021-05-25 14:58:33.88,
2021-05-25 14:59:01.66,
2021-05-25 14:59:03.93,
2021-05-25 14:59:06.11,
2021-05-25 14:59:06.86,
2021-05-25 14:59:07.99,
2021-05-25 14:59:31.67,
2021-05-25 14:59:40.04,
2021-05-25 14:59:41.94,
2021-05-25 15:00:14.97,
2021-05-25 14:59:38.03,
2021-05-25 14:59:40.80,
2021-05-25 15:00:01.67,
2021-05-25 15:00:11.98,
2021-05-25 15:00:15.74,
2021-05-25 15:00:31.67,
2021-05-25 15:00:46.91,
2021-05-25 15:00:50.11,
2021-05-25 15:01:01.68,
2021-05-25 15:01:10.09,
2021-05-25 15:01:10.09,
2021-05-25 15:01:10.21,
2021-05-25 15:01:10.21,
2021-05-25 15:01:10.21,
2021-05-25 15:01:10.21,
2021-05-25 15:06:17.58,
2021-05-25 15:06:18.61,
2021-05-25 15:06:18.98,
2021-05-25 13:25:50.14,
2021-05-25 13:25:50.14,
2021-05-25 13:25:50.15,
2021-05-25 13:25:50.15,
2021-05-25 13:25:50.10,
2021-05-25 13:26:20.23,
2021-05-25 13:26:21.23,
2021-05-25 13:26:21.23,
2021-05-25 13:26:21.23,
2021-05-25 13:26:21.23,
2021-05-25 13:26:21.23,
2021-05-25 13:26:21.23,
2021-05-25 13:26:21.25,
2021-05-25 13:26:23.24,
2021-05-25 13:26:24.25,
2021-05-25 13:26:24.25,
2021-05-25 13:26:24.25,
2021-05-25 09:33:50.07,
2021-05-26 08:53:44.44,
2021-05-26 08:53:44.44,
2021-05-26 08:53:44.44,
2021-05-26 08:53:44.44,
2021-05-26 08:53:49.95,
2021-05-26 08:53:49.95,
2021-05-26 08:53:49.95,
2021-05-26 08:53:52.46,
2021-05-26 08:53:52.46,
2021-05-26 08:53:52.46,
2021-05-26 08:53:52.46,
2021-05-26 08:53:57.47,
2021-05-26 08:53:57.48,
2021-05-26 08:53:57.47,
2021-05-26 08:53:57.49,
2021-05-26 08:53:57.47,
2021-05-26 08:55:47.78,
2021-05-25 10:07:34.21,
2021-05-25 10:21:15.28,
2021-05-25 10:34:43.88,
2021-05-26 11:58:36.39,
2021-05-26 11:59:06.51,
2021-05-26 11:59:29.10,
2021-05-26 11:59:29.11,
2021-05-26 12:00:36.37,
2021-05-26 12:01:06.49,
2021-05-26 12:01:36.10,
2021-05-26 12:01:37.03,
2021-05-26 12:01:37.03,
2021-05-26 12:01:37.03

Thank you.


Solution

  • Assuming the xts object in the Note at the end:

    library(xts)
    x6 <- to.hourly(x, k = 6); x6
    ##                     x.Open x.High x.Low x.Close
    ## 2021-05-25 13:26:24     68     88    51      67
    ## 2021-05-25 15:06:18      1     50     1      50
    ## 2021-05-26 12:01:37     69     98    69      98
    

    To extract the close use Cl from quantmod:

    library(quantmod)
    Cl(x6)
    ##                     x.Close
    ## 2021-05-25 13:26:24      67
    ## 2021-05-25 15:06:18      50
    ## 2021-05-26 12:01:37      98
    

    If rounded hours are needed then this uses xts align.time:

    to.hourly(align.time(x, n = 6 * 3600), k = 6)
    ##                     align.time(x, n = 6 * 3600).Close
    ## 2021-05-25 14:00:00                                67
    ## 2021-05-25 20:00:00                                50
    ## 2021-05-26 14:00:00                                98
    

    Note

    Lines <- "2021-05-25 14:56:45.19,
    2021-05-25 14:56:47.50,
    2021-05-25 14:56:48.28,
    2021-05-25 14:56:49.42,
    2021-05-25 14:57:01.64,
    2021-05-25 14:57:19.46,
    2021-05-25 14:57:21.87,
    2021-05-25 14:57:22.65,
    2021-05-25 14:57:23.79,
    2021-05-25 15:04:46.01,
    2021-05-25 15:04:46.38,
    2021-05-25 15:04:51.21,
    2021-05-25 15:04:51.58,
    2021-05-25 14:57:53.83,
    2021-05-25 14:57:57.40,
    2021-05-25 14:57:58.17,
    2021-05-25 14:57:59.31,
    2021-05-25 14:58:01.65,
    2021-05-25 14:58:29.34,
    2021-05-25 14:58:31.66,
    2021-05-25 14:58:32.17,
    2021-05-25 14:58:32.76,
    2021-05-25 14:58:33.88,
    2021-05-25 14:59:01.66,
    2021-05-25 14:59:03.93,
    2021-05-25 14:59:06.11,
    2021-05-25 14:59:06.86,
    2021-05-25 14:59:07.99,
    2021-05-25 14:59:31.67,
    2021-05-25 14:59:40.04,
    2021-05-25 14:59:41.94,
    2021-05-25 15:00:14.97,
    2021-05-25 14:59:38.03,
    2021-05-25 14:59:40.80,
    2021-05-25 15:00:01.67,
    2021-05-25 15:00:11.98,
    2021-05-25 15:00:15.74,
    2021-05-25 15:00:31.67,
    2021-05-25 15:00:46.91,
    2021-05-25 15:00:50.11,
    2021-05-25 15:01:01.68,
    2021-05-25 15:01:10.09,
    2021-05-25 15:01:10.09,
    2021-05-25 15:01:10.21,
    2021-05-25 15:01:10.21,
    2021-05-25 15:01:10.21,
    2021-05-25 15:01:10.21,
    2021-05-25 15:06:17.58,
    2021-05-25 15:06:18.61,
    2021-05-25 15:06:18.98,
    2021-05-25 13:25:50.14,
    2021-05-25 13:25:50.14,
    2021-05-25 13:25:50.15,
    2021-05-25 13:25:50.15,
    2021-05-25 13:25:50.10,
    2021-05-25 13:26:20.23,
    2021-05-25 13:26:21.23,
    2021-05-25 13:26:21.23,
    2021-05-25 13:26:21.23,
    2021-05-25 13:26:21.23,
    2021-05-25 13:26:21.23,
    2021-05-25 13:26:21.23,
    2021-05-25 13:26:21.25,
    2021-05-25 13:26:23.24,
    2021-05-25 13:26:24.25,
    2021-05-25 13:26:24.25,
    2021-05-25 13:26:24.25,
    2021-05-25 09:33:50.07,
    2021-05-26 08:53:44.44,
    2021-05-26 08:53:44.44,
    2021-05-26 08:53:44.44,
    2021-05-26 08:53:44.44,
    2021-05-26 08:53:49.95,
    2021-05-26 08:53:49.95,
    2021-05-26 08:53:49.95,
    2021-05-26 08:53:52.46,
    2021-05-26 08:53:52.46,
    2021-05-26 08:53:52.46,
    2021-05-26 08:53:52.46,
    2021-05-26 08:53:57.47,
    2021-05-26 08:53:57.48,
    2021-05-26 08:53:57.47,
    2021-05-26 08:53:57.49,
    2021-05-26 08:53:57.47,
    2021-05-26 08:55:47.78,
    2021-05-25 10:07:34.21,
    2021-05-25 10:21:15.28,
    2021-05-25 10:34:43.88,
    2021-05-26 11:58:36.39,
    2021-05-26 11:59:06.51,
    2021-05-26 11:59:29.10,
    2021-05-26 11:59:29.11,
    2021-05-26 12:00:36.37,
    2021-05-26 12:01:06.49,
    2021-05-26 12:01:36.10,
    2021-05-26 12:01:37.03,
    2021-05-26 12:01:37.03,
    2021-05-26 12:01:37.03"
    
    library(xts)
    tt <- scan(text = Lines, sep = ",", what = "")
    tt <- as.POSIXct(tt[tt != ""])
    x <- xts(seq_along(tt), tt)