1

Is there any best option to select all row except the first row in each group. I am using head() to select the first row in each group but have a situation where I need to output data except the first row in each group.

I prefer options using data.table package.

setDT(mtcars)
mtcars[order(cyl,mpg), head(.SD,2), by=.(cyl)]
chinsoon12
  • 25,005
  • 4
  • 25
  • 35
R007
  • 101
  • 1
  • 13
  • 3
    Does this answer your question? [data.table - select first n rows within group](https://stackoverflow.com/questions/34753050/data-table-select-first-n-rows-within-group) – rj-nirbhay May 25 '20 at 15:47
  • I reviewed that thread but I am looking for dynamic option to select all rows within-group except first row. There are some groups where there is no 2nd rows. – R007 May 25 '20 at 15:51

2 Answers2

2

If you use tail() and set n = -1 it will return all but the first row (see ?tail). You can use this in your command as follows:

mtcars[order(cyl, mpg), tail(.SD, -1), by = .(cyl)]
chinsoon12
  • 25,005
  • 4
  • 25
  • 35
JBGruber
  • 11,727
  • 1
  • 23
  • 45
1

Why not just subset .SD with -1?

mtcars[order(cyl, mpg), .SD[-1], by = .(cyl)]

If you're looking for all but the last row, there's also the .N special symbol.

mtcars[order(cyl, mpg), .SD[-.N], by = .(cyl)]
chinsoon12
  • 25,005
  • 4
  • 25
  • 35
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57