0

I'm hoping to sort a dataframe by it's individual groups based on it's individual column value. A sample dataframe is as follows:

Group Answer Frequency
A Apple 12
B Apple 8
A Orange 11
B Orange 3
A Banana 20
B Banana 5
A Jackfruit 10
B Jackfruit 9
A Pineapple 5
B Pineapple 3
A Pear 20
B Pear 20
df_sample = structure(list(Group = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 
                                               2L, 1L, 2L, 1L, 2L), .Label = c("A", "B"), class = "factor"), 
                           Answer = structure(c(1L, 1L, 4L, 4L, 2L, 2L, 3L, 3L, 6L, 
                                                6L, 5L, 5L), .Label = c("Apple", "Banana", "Jackfruit", "Orange", 
                                                                        "Pear", "Pineapple"), class = "factor"), Frequency = c(12L, 
                           8L, 11L, 3L, 20L, 5L, 10L, 9L, 5L, 3L, 20L, 20L)), class = "data.frame", row.names = c(NA, 
                           -12L))

I'm hoping to sort them based on the Group column and to output the top 3 answers based on the frequency for each group. The outcome should be the following table.

Group Answer (top 3) Frequency
A Pear 20
A Banana 20
A Apple 12
B Pear 20
B Jackfruit 9
B Apple 8

If I'm not wrong the code should be something alone the line of but I'm not sure how to complete it

df_sample %>% group_by(Group) %>% order(Frequency, decreasing = T)

Thanks!

Luther_Proton
  • 348
  • 1
  • 7
  • In `dplyr`, use the `arrange` function to order rows. You can do `df_sample %>% arrange(Group, desc(Frequency))` or `df_sample %>% group_by(Group) %>% arrange(desc(Frequency), .by_group = TRUE)`. – Gregor Thomas Aug 01 '22 at 03:35
  • To get top 3, use the `slice_max` utility function: `df_sample %>% group_by(Group) %>% slice_max(Frequency, n = 3)` – Gregor Thomas Aug 01 '22 at 03:36

0 Answers0