user1723765
user1723765

Reputation: 6409

Calculate frequency of occurrence in an array using R

I have an array

a <- c(1,1,1,1,1,2,3,4,5,5,5,5,5,6,7,7,7,7)

I would like to use some command that would tell me which is the most frequent number in the array?

is there a simple command for this?

Upvotes: 11

Views: 67655

Answers (4)

Rui Afonso Pereira
Rui Afonso Pereira

Reputation: 527

Use table() function:

## Your vector:
a <- c(1,1,1,1,1,2,3,4,5,5,5,5,5,6,7,7,7,7)

## Frequency table
> counts <- table(a)

## The most frequent and its value
> counts[which.max(counts)]
# 1
# 5

## Or simply the most frequent
> names(counts)[which.max(counts)]
# [1] "1"

Upvotes: 5

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193547

The table() function is sufficient for this, and particularly useful if your data have more than one mode.

Consider the following options, all related to table() and max().


# Your vector
a = c(1,1,1,1,1,2,3,4,5,5,5,5,5,6,7,7,7,7)

# Basic frequency table
table(a)
# a
# 1 2 3 4 5 6 7 
# 5 1 1 1 5 1 4 

# Only gives me the value for highest frequency
# Doesn't tell me which number that is though
max(table(a))
# [1] 5

# Gives me a logical vector, which might be useful
# but not what you're asking for in this question
table(a) == max(table(a))
# a
#    1     2     3     4     5     6     7 
# TRUE FALSE FALSE FALSE  TRUE FALSE FALSE 

# This is probably more like what you're looking for
which(table(a) == max(table(a)))
# 1 5 
# 1 5 

# Or, maybe this
names(which(table(a) == max(table(a))))
# [1] "1" "5"

As indicated in the comments, in some cases you might want to see the two or three most commonly occurring values, in which case sort() is useful:

sort(table(a))
# a
# 2 3 4 6 7 1 5 
# 1 1 1 1 4 5 5 

You can also set a threshold for which values to return in your table. For instance, if you wanted to return only those numbers which occurred more than once:

sort(table(a)[table(a) > 1])
# a
# 7 1 5 
# 4 5 5 

Upvotes: 31

Carl Witthoft
Carl Witthoft

Reputation: 21502

I wrote some personal code to find the mode and a little more (a few years ago. As Ananda showed, it's pretty obvious stuff) :

smode<-function(x){
    xtab<-table(x)
    modes<-xtab[max(xtab)==xtab]
    mag<-as.numeric(modes[1]) #in case mult. modes, this is safer
    #themodes<-names(modes)
    themodes<-as.numeric(names(modes))
    mout<-list(themodes=themodes,modeval=mag)
    return(mout)
    }

Blah blah copyright blah blah use as you like but don't make money off it.

Upvotes: 2

RoryB
RoryB

Reputation: 1047

What you want is the mode of the data: there are a variety of different options for calculating it. The modeest package has a set of functions for mode estimation, but might be overkill for what you want.

See also:

Is there a built-in function for finding the mode?

How to compute conditional Mode in R?

Hope that helps

Upvotes: 0

Related Questions