I would like to subset a data frame for n rows, which are grouped by a variable and are sorted descending by another variable. This would be clear with an example:
d1 <- data.frame(Gender = c("M", "M", "F", "F", "M", "M", "F",
"F"), Age = c(15, 38, 17, 35, 26, 24, 20, 26))
I would like to get 2 rows, which are sorted descending on Age, for each Gender. The desired output is:
Gender Age
F 35
F 26
M 38
M 26
I looked for order, sort and other solutions here, but could not find an appropriate solution to this problem. I appreciate your help.