pedrosaurio
pedrosaurio

Reputation: 4926

How to get the top n elements (in terms of frequency) from a vector in R?

How can I get the top n ranking of an array in R?

lets say I have

a <- c(67, 2, 100, 2, 100, 23, 2, 100, 67, 89,100)

how can I get:

rank   number   times
1     100       4
2     2         3
3     67        2
4     23        1
4     89        1

Upvotes: 9

Views: 11917

Answers (6)

markdly
markdly

Reputation: 4534

A dplyr solution to this could be:

library(dplyr)
df <- tibble(a = c(67, 2, 100, 2, 100, 23, 2, 100, 67, 89,100))
df %>% 
  count(a) %>% 
  mutate(rank = min_rank(-n)) %>%
  arrange(desc(n)) %>% 
  rename(number = a, times = n)
#> # A tibble: 5 x 3
#>   number times  rank
#>    <dbl> <int> <int>
#> 1    100     4     1
#> 2      2     3     2
#> 3     67     2     3
#> 4     23     1     4
#> 5     89     1     4

Upvotes: 1

Juan Carlos
Juan Carlos

Reputation: 1

you can use df[df>0] <- 1, later rowSums(df), and finally with(df, df[order(-x, y, z), ] where -x is the column of frequency data and the others are I.D columns, and the suplementary information that you have.

Upvotes: 0

Andrie
Andrie

Reputation: 179398

Use table with sort:

sort(table(a), decreasing=TRUE)
a
100   2  67  23  89 
  4   3   2   1   1 

If you want to convert the results to a data frame, then simply wrap all of this into data.frame():

data.frame(count=sort(table(a), decreasing=TRUE))
    count
100     4
2       3
67      2
23      1
89      1

Upvotes: 7

Paul Hiemstra
Paul Hiemstra

Reputation: 60924

Or use count from the plyr package:

require(plyr)
df = count(a)
df[order(df[["freq"]], decreasing = TRUE),] 
    x freq
5 100    4
1   2    3
3  67    2
2  23    1
4  89    1

Upvotes: 1

Roland
Roland

Reputation: 132676

tab <- table(a<-c(67, 2, 100, 2, 100, 23, 2, 100, 67, 89,100))
df <- as.data.frame(tab)
names(df) <- c("number","times")
df$rank <- rank(-df$times,ties.method="min")
df <- df[order(df$rank,decreasing = F),]
df
  number times rank
5    100     4    1
1      2     3    2
3     67     2    3
2     23     1    4
4     89     1    4

Upvotes: 11

dickoa
dickoa

Reputation: 18437

You can try something like this :

a <- c(67, 2, 100, 2, 100, 23, 2, 100, 67, 89,100)
DF <- as.data.frame(table(a))

DF[order(DF[,2], decreasing = TRUE), ]
    a Freq
5 100    4
1   2    3
3  67    2
2  23    1
4  89    1

Upvotes: 3

Related Questions