Suppose I have a dataframe:
value = c(1:5,16:20, 26:30)
group = c(rep("A", 5), rep("B", 5), rep("C", 5))
df = data.frame(value, group)
I would like to create a new dataframe that would contain top_n values for each group such that n for A group = 3, n for B group = 2 and n for C group = 1.
# new dataframe should look like this:
value group
1 5 A
2 4 A
3 3 A
4 20 B
5 19 B
6 30 C
I think I should map top_n function to my data, but I am struggling to find the right implementation.