christopherhlee
christopherhlee

Reputation: 99

R - How to order list by ascending/descending frequency?

I have a categorical variable with 191 unique values (smartphone models). How do I create a list or data frame that orders these model names in ascending/descending order by frequency?

Here's an example of the first few rows of the Model variable.

table(full2$Model)


             10                 105                  3T              5 Plus 
             19                   1                   5                   9 
             5T                   6                   8                  A1 
             19                   1                   8                   2 
          A3003                  A5                  A9              AXON 7 
              1                   1                   2                   1 
     Black Moon                Bolt            Convoy 3            Cosmos 3 
             21                   1                   2                   5 
     Desire 610          Desire 625             Droid 2         Droid Turbo 
              6                   1                   1                  63 

Ultimately, I need only the top X number of models.

Upvotes: 0

Views: 3351

Answers (1)

P1storius
P1storius

Reputation: 947

You can use table() for this

sort(table(full2$Model))

To get the first 10 entries:

sort(table(full2$Model))[1:10]

To reverse sort order:

sort(table(full2$Model), decreasing=TRUE)[1:10]

Upvotes: 3

Related Questions