5

I am trying to filter a data frame that contains n rows for n categories. I want that each category dimension values are sorted by another column revenues and then, the top 10 values of each dimension are selected and get rid of the rest.

I did an attempt with the following snippet, but it does not seem to achieve what I want to:

data <-  tbl_df(data) %>%
  arrange(revenues) %>%
  group_by(dimension) %>%
  top_n(10)
5
  • could you share the source of error ? Otherwise we won't be able to help you
    – linog
    Apr 10, 2020 at 11:38
  • I don't get an error. I am just not getting the desired output.
    – sanna
    Apr 10, 2020 at 11:42
  • 1
    Does this answer your question? Getting the top values by group
    – DJJ
    Apr 10, 2020 at 15:23
  • It seems to be a duplicate. When posting a question I would recommend avoiding putting dplyr at the front of the title unless you are a master of dplyr. It might save you having to write the question entirely.
    – DJJ
    Apr 10, 2020 at 15:30
  • Does this answer your question? Select the top N values by group
    – camille
    Sep 21, 2021 at 2:18

3 Answers 3

9
data <-  tbl_df(data) %>%
  group_by(dimension) %>%
  arrange(revenues, .by_group = TRUE) %>%
  top_n(10)
2
  • You need to specify which column to be ordered by in top_n(), or it'll take the default to the last variable in the data. Apr 10, 2020 at 12:04
  • 2
    For late comers, top_n has been superceded by slice_min or slice_max to be clear on order. Not threatened to be deprecated as at dplyr 1.0.10. Safer syntax for future would be slice_max(revenues, n = 10) for ten largest revenues and for smallest ones use slice_min. Slice has a number of alternatives. You can also slice on existing database order slice_head, or if you wanted a random sample tilted to bigger revenue items, assuming replacement ,slice_sample(n = 10, replace = TRUE, weight_by = revenues).
    – micstr
    May 30, 2023 at 13:09
2

We can test it with an example data:

set.seed(100)
data = data.frame(revenues=rnbinom(100,mu=1000,size=1),
dimension=sample(letters[1:2],100,replace=TRUE))

Firstly as @DarrenTsai correctly pointed out, you need to specify the column to do top_n(). Secondly, when you use top_n, it goes by descending order and takes the entries with rank 1-10:

data %>% top_n(10,revenues)
   revenues dimension
1      4191         b
2      1916         a
3      2397         b
4      1895         a
5      2013         a
6      2351         b
7      3889         b
8      2503         a
9      3909         a
10     2779         b

This means you don't need to arrange your data, and I am not sure whether you intend to take it in descending or ascending. Let's assume it is descending, :

data %>%  group_by(dimension) %>% top_n(10,revenues)

Note, this code above will take the top 10 values, meaning in events of ties (say you have 2 ranked 1st), you will get more than 10. For example in this data:

# A tibble: 21 x 2
# Groups:   dimension [2]
   revenues dimension
      <dbl> <fct>    
 1     1663 a        
 2     1663 a        
 3     1753 a        
 4     1849 a        
 5     1856 a        
 6     1869 a        
 7     1895 a        
 8     1916 a        
 9     2013 a        
10     2503 a        
# … with 11 more rows

We can see whether the results are correct, this is what we expect:

unlist(tapply(data$revenues,data$dimension,function(i)-sort(-i)[1:10]))
  a1   a2   a3   a4   a5   a6   a7   a8   a9  a10   b1   b2   b3   b4   b5   b6 
3909 2503 2013 1916 1895 1869 1856 1849 1753 1663 4191 3889 2779 2397 2351 1479 
  b7   b8   b9  b10 
1414 1340 1327 1274 

And using the group_by + top_n() :

 data %>%  group_by(dimension) %>% top_n(10,revenues) %>% 
arrange(dimension,desc(revenues)) %>% pull(revenues)
 [1] 3909 2503 2013 1916 1895 1869 1856 1849 1753 1663 1663 4191 3889 2779 2397
[16] 2351 1479 1414 1340 1327 1274

You can see 1663 is taken twice, giving 21 values in total.

If you need absolutely 20 (10 each):

data %>% arrange(desc(revenues)) %>%
group_by(dimension) %>% do(head(.,10))
1
data <-  tbl_df(data) %>%
  group_by(dimension) %>%
  arrange(desc(revenues),.by_group=TRUE) %>%
  top_n(10,revenues)
2
  • 2
    Welcome to stackoverflow and thanks for your sharing your answer. To make your answer complete It is essential you add some explanation to your answer, Feel free give some identify what was the issue with the original post.
    – DJJ
    Apr 10, 2020 at 15:20
  • Thank you and I shall keep in mind to add necessary explanation going forward. Apr 12, 2020 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.