Calculate frequency of occurrence in an array using R
r
Solution
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
Problem
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?