从 tsibble 对象中检索最后 n 行 - R

Retrieve last n rows from tsibble object - R

我有一个 tsibble 矩阵,每天从传入数据中增长 32 行,我只想在我的绘图函数中绘制过去 5 天,这需要我对 (32*5) 160 进行子集化底部的行。随着每日新数据的到来,每行的日期每 32 行更改一次。

例如


    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)

我只能使用函数检索最后 5 行


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

虽然我应该 n = 6 我得到这个错误。

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

关于如何解决此问题的任何见解或想法?

OG 数据


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"))


所以日期是这样分组的,而不是交替的,而是按代码分组的。

通过使用这段简单的代码,我能够避免在创建原始数据集时遇到的错误。不是修复错误,而是解决错误的方法。

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