Given a dataframe:
df <- structure(list(a = c(1, 1, 1, 2, 2, 2, 3, 3, 4, 4), b = c(34,
343, 54, 11, 55, 62, 59, -9, 0, -0.5)), row.names = c(NA, -10L
), class = c("tbl_df", "tbl", "data.frame"))
I want to take last N observations / rows from each group:
df %>%
dplyr::group_by(a) %>%
dplyr::last(2)
Gives me wrong results.
I want it to be:
a b
1 343
1 54
2 55
2 62
3 59
3 -9
4 0
4 -0.5
Please advise what is wrong here?
The error I get is:
Error in order(order_by)[[n]] : subscript out of bounds
Also a
tidyverse
possibility:It is taking the top two rows given the row numbers per groups.
As it is a specific question based on
dplyr
1) after the
group_by
, useslice
on therow_number()
2) Or use
filter
fromdplyr
3) or with
do
andtail
4) In addition to the
tidyverse
, methods, we can also use compactdata.table
5) Or
by
frombase R
6) or with
aggregate
frombase R
7) or with
split
frombase R
A base R option using
tapply
is to subset the last two rows for every group.Or another option using
ave
try tail().In R head function allows you to preview the first n rows whike tail allows you to preview last n rows