简体   繁体   中英

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
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

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

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

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

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

您可以使用df[df>0] <- 1 ,稍后使用rowSums(df) ,最后with(df, df[order(-x, y, z), ] ,其中-x是频率数据列和其他是ID列,以及您拥有的完整信息。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM