display top 10 rows after arrange verb

I want to only display top 10 rows of individual_score on the below code. I am unable to achieve that. I tried top_n() after arrange but that is not working. I also tried to filter (individual_score == top_n()) but that did not work either.Can anyone help me how to achieve only 10 rows. With the code below it results 11,294 rows

highest_individual_score <- innings%>%
group_by(match_id,batsman)%>%
summarize(individual_score = sum(batsman_runs))%>%
arrange(desc(individual_score))

I think you just need to add the n argument to top_n.

highest_individual_score <- innings%>%
  group_by(match_id,batsman)%>%
  summarize(individual_score = sum(batsman_runs))%>%
  arrange(desc(individual_score)) %>%
  top_n(10)

I tried that before but doesn't work.

You can use head:

highest_individual_score <- innings%>%
  group_by(match_id,batsman)%>%
  summarize(individual_score = sum(batsman_runs))%>%
  arrange(desc(individual_score)) %>%
  head(n = 10L)

I don't know why top_n doesn't work, but I'll guess the following to be the reason:

wt (Optional). The variable to use for ordering. If not specified, defaults to the last variable in the tbl.

I also don't know why it didn't work. I saw couple of suggestions on internet for top_n but it did not work. head(n =10) worked fine.

Thank you for the input

There are a couple of things going on here. First, as @Yarnabrina mentioned, you need to provide top_n with the column to use for ranking. In your case, it would be (assuming you want the top 10 rows based on individual_score):

highest_individual_score <- innings%>%
 group_by(match_id, batsman)%>%
 summarize(individual_score = sum(batsman_runs))%>%
 top_n(10, individual_score)

However, that will probably give you more than 10 rows, because your data are grouped by match after the summarize step. With grouped data, top_n will return the top 10 for each group. If you want the top 10 rows of the overall data frame then ungroup first:

highest_individual_score <- innings%>%
 group_by(match_id, batsman)%>%
 summarize(individual_score = sum(batsman_runs))%>%
 ungroup %>%
 top_n(10, individual_score)

Or, with your original code, since you've sorted the data frame, you could do:

highest_individual_score <- innings%>%
  group_by(match_id, batsman)%>%
  summarize(individual_score = sum(batsman_runs))%>%
  arrange(desc(individual_score)) %>%
  ungroup %>%
  slice(1:10)

@Yarnabrina's method of using head works as well. However, this might in general give unexpected results (within the tidyverse), because head doesn't respect grouping.

1 Like

Thanks. Ungroup was missing on my code. slice and top_n both works after ungroup. Head works as well. Like @joels said head might give unexpected result i will use slice for my code

Thank you all for inputs

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.