Home > Blockchain >  Slice last and last-2 rows for each group
Slice last and last-2 rows for each group

Time:05-20

Let;s say I have data set and would like to select in grouping 3rd and 5th rows:

   df <- data.table(
     x = c(1, 2, 3, 4, 5),
     d = c("2022-05-01", "2022-05-02", "2022-05-03", "2022-05-04", "2022-05-05"))

I need to select by using slice() and row_number() only "3" and "5" rows.

df %>% 
  group_by(d) %>% 
  arrange(d) %>%
  slice(tail(row_number()-2, row_number())) %>%
  ungroup()

CodePudding user response:

Using data.table with maybe a better example data, try this:

library(data.table)

# example data
x <- mtcars[mtcars$cyl %in% c(4, 6), c("cyl", "mpg")]
setDT(x)

# order by mpg and get Nth and Nth-2 rows per cyl
x[order(mpg), .SD[ { ind = (.N - c(2, 0)); ind[ind > 0]; }, ], by = cyl ]
#    cyl  mpg
# 1:   6 21.0
# 2:   6 21.4
# 3:   4 30.4
# 4:   4 33.9

CodePudding user response:

Like this? It should work with group_by()

library(dplyr)
df %>% 
  slice(n()-2, n())
  • Related