Retrieve last n rows from tsibble object - R

487 views Asked by At

I have a tsibble matrix that grows 32 rows larger every day from incoming data, I would like to only plot the past 5 days in my plotting function which requires me to subset (32*5) 160 of the bottom rows. The dates for each row change every 32 rows, as new daily data comes in.

E.g


    library(tsibble)
    library(lubridate)
    df <- data.frame(ticker = c("UST10Y", "UST2Y", "AAPL", "SPX", "BNO"),
                     buy_price = c(62.00, 68.00, 37.00, 55.00, 41.00),
                     sale_price = c(64.00, 71.00, 42.00, 60.00, 45.00),
                     close_price = c(63.00, 70.00, 38.00, 56.00, 43.00),
                     date = mdy(c("April 29th, 2021", "April 29th, 2021", "April 29th, 2021", "April 29th, 2021", "April 29th, 2021")))
    
    df2 <- data.frame(ticker = c("UST10Y", "UST2Y", "AAPL", "SPX", "BNO"),
                      buy_price = c(63.00, 69.00, 38.00, 53.00, 44.00),
                      sale_price = c(66.00, 77.00, 47.00, 63.00, 48.00),
                      close_price = c(65.00, 74.00, 39.00, 55.00, 45.00),
                      date = mdy(c("April 30th, 2021", "April 30th, 2021", "April 30th, 2021", "April 30th, 2021", "April 30th, 2021")))
    
    df3 <- data.frame(ticker = c("UST10Y", "UST2Y", "AAPL", "SPX", "BNO"),
                      buy_price = c(63.00, 69.00, 38.00, 53.00, 44.00),
                      sale_price = c(66.00, 77.00, 47.00, 63.00, 48.00),
                      close_price = c(65.00, 74.00, 39.00, 55.00, 45.00),
                      date = mdy(c("May 1st, 2021", "May 1st, 2021", "May 1st, 2021", "May 1st, 2021", "May 1st, 2021")))
    
    final_df <- rbind(df,df2, df3)
    as_tsibble(final_df, index = date, key = ticker, regular = T)

I have only been able to retrieve the last 5 rows with the functions


    final_df %>%  
        slice_tail(n = 5)
    
    tail(final_df, 5)

Although should I make n = 6 I get this error.

Error: Can't obtain the interval due to the mismatched index class.
i Please see `vignette("FAQ")` for details.

Any insight or ideas on how to fix this?

OG Data


rr_master_tsibble_rep <-
        structure(
                list(
                        DATE = structure(
                                c(
                                        18751,
                                        18752,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18751,
                                        18750,
                                        18751,
                                        18750,
                                        18751,
                                        18750,
                                        18751,
                                        18750,
                                        18751,
                                        18750,
                                        18751,
                                        18750,
                                        18751,
                                        18750,
                                        18751,
                                        18750,
                                        18751
                                ),
                                class = "Date"
                        ),
                        TICKER = c(
                                "AAPL ",
                                "AAPL ",
                                "AMZN ",
                                "CAD/USD ",
                                "COMPQ ",
                                "COPPER ",
                                "DAX ",
                                "EUR/USD ",
                                "FB ",
                                "GBP/USD ",
                                "GOLD ",
                                "GOOGL ",
                                "MSFT ",
                                "NATGAS ",
                                "NFLX ",
                                "NIKK ",
                                "NYXBT ",
                                "RUT ",
                                "SILVER ",
                                "SPX ",
                                "SSEC ",
                                "TSLA ",
                                "USD ",
                                "USD/CHF ",
                                "USD/JPY ",
                                "UST10Y ",
                                "UST10Y ",
                                "UST2Y ",
                                "UST2Y ",
                                "VIX ",
                                "VIX ",
                                "WTIC ",
                                "WTIC ",
                                "XLE ",
                                "XLE ",
                                "XLF ",
                                "XLF ",
                                "XLK ",
                                "XLK ",
                                "XLU ",
                                "XLU "
                        ),
                        BUY.TRADE = c(
                                131,
                                127,
                                3335,
                                0.79,
                                13801,
                                4.26,
                                15106,
                                1.198,
                                310,
                                1.38,
                                1753,
                                2310,
                                248,
                                2.72,
                                483,
                                28431,
                                50209,
                                2227,
                                25.75,
                                4140,
                                3407,
                                657,
                                90.3,
                                0.9,
                                107.45,
                                1.71,
                                1.72,
                                0.19,
                                0.18,
                                15.74,
                                15.8,
                                62.04,
                                62.2,
                                46.73,
                                46.75,
                                35.03,
                                35.22,
                                139.22,
                                138.45,
                                65.24,
                                65.29
                        ),
                        SELL.TRADE = c(
                                136,
                                137,
                                3521,
                                0.82,
                                14204,
                                4.62,
                                15463,
                                1.216,
                                334,
                                1.401,
                                1797,
                                2407,
                                264,
                                3.06,
                                520,
                                29840,
                                60982,
                                2339,
                                27.09,
                                4229,
                                3498,
                                745,
                                91.43,
                                0.92,
                                109.74,
                                1.57,
                                1.56,
                                0.15,
                                0.15,
                                19.12,
                                18.98,
                                65.16,
                                65.7,
                                50.99,
                                51.91,
                                36.9,
                                37.01,
                                144.08,
                                144.46,
                                67.88,
                                67.45
                        ),
                        PREV.CLOSE = c(
                                132,
                                127,
                                3386,
                                0.81,
                                13895,
                                4.53,
                                15236,
                                1.206,
                                322,
                                1.391,
                                1791,
                                2343,
                                251,
                                2.97,
                                509,
                                28812,
                                58035,
                                2277,
                                26.96,
                                4192,
                                3446,
                                684,
                                90.93,
                                0.91,
                                109.07,
                                1.65,
                                1.63,
                                0.16,
                                0.16,
                                18.61,
                                18.31,
                                63.58,
                                64.49,
                                49.39,
                                50.75,
                                36.26,
                                36.44,
                                139.7,
                                139.31,
                                66.72,
                                66.71
                        ),
                        TREND = structure(
                                c(
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        1L,
                                        2L,
                                        2L,
                                        2L,
                                        1L,
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        1L,
                                        1L,
                                        1L,
                                        1L,
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        1L,
                                        1L,
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        2L,
                                        3L,
                                        3L
                                ),
                                .Label = c("BEARISH", "BULLISH",
                                           "NEUTRAL"),
                                class = "factor"
                        )
                ),
                row.names = c(NA,-41L),
                key = structure(
                        list(
                                TICKER = c(
                                        "AAPL ",
                                        "AMZN ",
                                        "CAD/USD ",
                                        "COMPQ ",
                                        "COPPER ",
                                        "DAX ",
                                        "EUR/USD ",
                                        "FB ",
                                        "GBP/USD ",
                                        "GOLD ",
                                        "GOOGL ",
                                        "MSFT ",
                                        "NATGAS ",
                                        "NFLX ",
                                        "NIKK ",
                                        "NYXBT ",
                                        "RUT ",
                                        "SILVER ",
                                        "SPX ",
                                        "SSEC ",
                                        "TSLA ",
                                        "USD ",
                                        "USD/CHF ",
                                        "USD/JPY ",
                                        "UST10Y ",
                                        "UST2Y ",
                                        "VIX ",
                                        "WTIC ",
                                        "XLE ",
                                        "XLF ",
                                        "XLK ",
                                        "XLU "
                                ),
                                .rows = structure(
                                        list(
                                                1:2,
                                                3L,
                                                4L,
                                                5L,
                                                6L,
                                                7L,
                                                8L,
                                                9L,
                                                10L,
                                                11L,
                                                12L,
                                                13L,
                                                14L,
                                                15L,
                                                16L,
                                                17L,
                                                18L,
                                                19L,
                                                20L,
                                                21L,
                                                22L,
                                                23L,
                                                24L,
                                                25L,
                                                26:27,
                                                28:29,
                                                30:31,
                                                32:33,
                                                34:35,
                                                36:37,
                                                38:39,
                                                40:41
                                        ),
                                        ptype = integer(0),
                                        class = c("vctrs_list_of",
                                                  "vctrs_vctr", "list")
                                )
                        ),
                        row.names = c(NA, 32L),
                        class = c("tbl_df",
                                  "tbl", "data.frame"),
                        .drop = TRUE
                ),
                index = structure("DATE", ordered = TRUE),
                index2 = "DATE",
                interval = structure(
                        list(
                                year = 0,
                                quarter = 0,
                                month = 0,
                                week = 0,
                                day = 1,
                                hour = 0,
                                minute = 0,
                                second = 0,
                                millisecond = 0,
                                microsecond = 0,
                                nanosecond = 0,
                                unit = 0
                        ),
                        .regular = TRUE,
                        class = c("interval",
                                  "vctrs_rcrd", "vctrs_vctr")
                ),
                class = c("tbl_ts", "tbl_df", "tbl",
                          "data.frame"))


So the dates are grouped like that and not alternating and grouped by ticker.

1

There are 1 answers

0
JJ Fantini On BEST ANSWER

I was able to circumvent the error I was encountering from the creation of my original data set by using this simple bit of code. Not a fix to the error, but a way around it.

    final_df_6 <- final_df %>%
            as.data.frame() %>%
            slice_tail(n = 6) %>% 
            as_tsibble(index = DATE, key = TICKER)