Make a table showing the 10 largest values of a variable in R?

前端 未结 4 2066
温柔的废话
温柔的废话 2021-02-04 15:03

I want to make a simple table that showcases the largest 10 values for a given variable in my dataset, as well as 4 other variables for each observation, so basically a small su

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-04 15:57

    You can do this using arrange from dplyr. This should also work if there are grouping variables. Just add group_by before the arrange. We filter the first 10 observations using slice.

     library(dplyr)
     df1 %>%
        arrange(desc(Score)) %>%
        slice(1:10) 
    

    Or another option is ?top_n (commented by @docendodiscimus) from dplyr which is a wrapper that uses filter and min_rank to select the top n (i.e. 10) entries for 'Score'.

     top_n(df1, 10, Score)    
    

    Or we use filter by creating a logical condition with row_number which is equivalent to rank(ties.method='first') (contributed by @Steven Beaupre)

     filter(df1, row_number(desc(Score)) <= 10)
    

    Or a data.table option (by @David Arenburg). We convert the 'data.frame' to 'data.table' (setDT(df1)), order (decreasing) the 'Score' variable, and select the first 10 observations. .SD means Subset of DataTable.

     library(data.table)
     setDT(df1)[order(-Score), .SD[1:10]]
    

提交回复
热议问题