title: Retrieve last n rows from tsibble object - R
body: <p>I have a <code>tsibble</code> 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.</p>
  <p>E.g</p>
  <pre><code>
      library(tsibble)
      library(lubridate)
      df &lt;- data.frame(ticker = c(&quot;UST10Y&quot;, &quot;UST2Y&quot;, &quot;AAPL&quot;, &quot;SPX&quot;, &quot;BNO&quot;),
                       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(&quot;April 29th, 2021&quot;, &quot;April 29th, 2021&quot;, &quot;April 29th, 2021&quot;, &quot;April 29th, 2021&quot;, &quot;April 29th, 2021&quot;)))
      
      df2 &lt;- data.frame(ticker = c(&quot;UST10Y&quot;, &quot;UST2Y&quot;, &quot;AAPL&quot;, &quot;SPX&quot;, &quot;BNO&quot;),
                        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(&quot;April 30th, 2021&quot;, &quot;April 30th, 2021&quot;, &quot;April 30th, 2021&quot;, &quot;April 30th, 2021&quot;, &quot;April 30th, 2021&quot;)))
      
      df3 &lt;- data.frame(ticker = c(&quot;UST10Y&quot;, &quot;UST2Y&quot;, &quot;AAPL&quot;, &quot;SPX&quot;, &quot;BNO&quot;),
                        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(&quot;May 1st, 2021&quot;, &quot;May 1st, 2021&quot;, &quot;May 1st, 2021&quot;, &quot;May 1st, 2021&quot;, &quot;May 1st, 2021&quot;)))
      
      final_df &lt;- rbind(df,df2, df3)
      as_tsibble(final_df, index = date, key = ticker, regular = T)
  
  </code></pre>
  <p>I have only been able to retrieve the last 5 rows with the functions</p>
  <pre><code>
      final_df %&gt;%  
          slice_tail(n = 5)
      
      tail(final_df, 5)
  
  </code></pre>
  <p>Although should I make <code>n = 6</code> I get this error.</p>
  <pre><code>Error: Can't obtain the interval due to the mismatched index class.
  i Please see `vignette(&quot;FAQ&quot;)` for details.
  </code></pre>
  <p>Any insight or ideas on how to fix this?</p>
  <p>OG Data</p>
  <pre><code>
  rr_master_tsibble_rep &lt;-
          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 = &quot;Date&quot;
                          ),
                          TICKER = c(
                                  &quot;AAPL &quot;,
                                  &quot;AAPL &quot;,
                                  &quot;AMZN &quot;,
                                  &quot;CAD/USD &quot;,
                                  &quot;COMPQ &quot;,
                                  &quot;COPPER &quot;,
                                  &quot;DAX &quot;,
                                  &quot;EUR/USD &quot;,
                                  &quot;FB &quot;,
                                  &quot;GBP/USD &quot;,
                                  &quot;GOLD &quot;,
                                  &quot;GOOGL &quot;,
                                  &quot;MSFT &quot;,
                                  &quot;NATGAS &quot;,
                                  &quot;NFLX &quot;,
                                  &quot;NIKK &quot;,
                                  &quot;NYXBT &quot;,
                                  &quot;RUT &quot;,
                                  &quot;SILVER &quot;,
                                  &quot;SPX &quot;,
                                  &quot;SSEC &quot;,
                                  &quot;TSLA &quot;,
                                  &quot;USD &quot;,
                                  &quot;USD/CHF &quot;,
                                  &quot;USD/JPY &quot;,
                                  &quot;UST10Y &quot;,
                                  &quot;UST10Y &quot;,
                                  &quot;UST2Y &quot;,
                                  &quot;UST2Y &quot;,
                                  &quot;VIX &quot;,
                                  &quot;VIX &quot;,
                                  &quot;WTIC &quot;,
                                  &quot;WTIC &quot;,
                                  &quot;XLE &quot;,
                                  &quot;XLE &quot;,
                                  &quot;XLF &quot;,
                                  &quot;XLF &quot;,
                                  &quot;XLK &quot;,
                                  &quot;XLK &quot;,
                                  &quot;XLU &quot;,
                                  &quot;XLU &quot;
                          ),
                          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(&quot;BEARISH&quot;, &quot;BULLISH&quot;,
                                             &quot;NEUTRAL&quot;),
                                  class = &quot;factor&quot;
                          )
                  ),
                  row.names = c(NA,-41L),
                  key = structure(
                          list(
                                  TICKER = c(
                                          &quot;AAPL &quot;,
                                          &quot;AMZN &quot;,
                                          &quot;CAD/USD &quot;,
                                          &quot;COMPQ &quot;,
                                          &quot;COPPER &quot;,
                                          &quot;DAX &quot;,
                                          &quot;EUR/USD &quot;,
                                          &quot;FB &quot;,
                                          &quot;GBP/USD &quot;,
                                          &quot;GOLD &quot;,
                                          &quot;GOOGL &quot;,
                                          &quot;MSFT &quot;,
                                          &quot;NATGAS &quot;,
                                          &quot;NFLX &quot;,
                                          &quot;NIKK &quot;,
                                          &quot;NYXBT &quot;,
                                          &quot;RUT &quot;,
                                          &quot;SILVER &quot;,
                                          &quot;SPX &quot;,
                                          &quot;SSEC &quot;,
                                          &quot;TSLA &quot;,
                                          &quot;USD &quot;,
                                          &quot;USD/CHF &quot;,
                                          &quot;USD/JPY &quot;,
                                          &quot;UST10Y &quot;,
                                          &quot;UST2Y &quot;,
                                          &quot;VIX &quot;,
                                          &quot;WTIC &quot;,
                                          &quot;XLE &quot;,
                                          &quot;XLF &quot;,
                                          &quot;XLK &quot;,
                                          &quot;XLU &quot;
                                  ),
                                  .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(&quot;vctrs_list_of&quot;,
                                                    &quot;vctrs_vctr&quot;, &quot;list&quot;)
                                  )
                          ),
                          row.names = c(NA, 32L),
                          class = c(&quot;tbl_df&quot;,
                                    &quot;tbl&quot;, &quot;data.frame&quot;),
                          .drop = TRUE
                  ),
                  index = structure(&quot;DATE&quot;, ordered = TRUE),
                  index2 = &quot;DATE&quot;,
                  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(&quot;interval&quot;,
                                    &quot;vctrs_rcrd&quot;, &quot;vctrs_vctr&quot;)
                  ),
                  class = c(&quot;tbl_ts&quot;, &quot;tbl_df&quot;, &quot;tbl&quot;,
                            &quot;data.frame&quot;))
  
  
  </code></pre>
  <p>So the dates are grouped like that and not alternating and grouped by ticker.</p>