You are currently viewing R Count Frequency of All Unique Values in Vector

There are multiple ways to get the count of the frequency of all unique values in an R vector. To count the number of times each element or value is present in a vector use either table(), tabulate(), count() from plyr package, or aggregate() function.

1. Quick Examples of Getting Frequency of all values in Vector

Following are quick examples of how to get the count or frequency of all unique values in a vector.


# Quick Examples

# Create Vector
x <- c(1,2,6,7,3,4,1,2,1,2,2)

# Frequency of all elements in vector
table(x)

# Get frequency of all values into data.frame
as.data.frame(table(x))

# Using rle() and sort()
rle(sort(x))

# Using tabulate()
tabulate(x)

# Using count() from plyr package
library('plyr')
count(x)

# Using vctrs package
library('vctrs')
vec_count(x)
vec_count(x, sort = "key")

# Using aggregate
aggregate(x, list(num=x), length)

1. Get Count of Frequency of All Unique Values in Vector

table() is an R base function that can take the vector as an input and returns the count of the frequency of all unique values in a vector.


# Frequency of all elements in vector
table(x)

Yields below output.


x
1 2 3 4 6 7 
3 4 1 1 1 1

2. Get Frequency of All Values into DataFrame

In case you wanted to get the frequency count of values in a vector as an R dataframe use as.data.frame() function. This takes the result of table() function as input and returns a dataframe.


# Get frequency of all values into data.frame
as.data.frame(table(x))

Yields below output.


# Output
  x Freq
1 1    3
2 2    4
3 3    1
4 4    1
5 6    1
6 7    1

3. Using rle() and sort()

Use rle() to return a values (the label, x in your example) and a lengths, which represents how many times that value appeared in sequence.

By combining rle with sort, you have an extremely fast way to count the number of times any value appeared. This can be helpful with more complex problems.


# Using rle() and sort()
rle(sort(x))

Yields below output.


# Output
Run Length Encoding
  lengths: int [1:6] 3 4 1 1 1 1
  values : num [1:6] 1 2 3 4 6 7

4. Using tabulate()

tabulate() is a standard function to get the number of times a unique value present in a vector. This returns a vector with the counts.


# Using tabulate()
tabulate(x)

Yields below output.


# Output
#[1] 3 4 1 1 0 1 1

5. Using aggregate()

aggregate() is another function


# Using aggregate
aggregate(x, list(num=x), length)

Yields below output.


# Output
  num x
1   1 3
2   2 4
3   3 1
4   4 1
5   6 1
6   7 1

5. Using plyr Package

You can use the third-party package plyr and its method count() to calculate the frequency count of each value in an R vector. If you don’t have this package, you can easily install the package and load it using the library('plyr')


# Using count() from plyr package
library('plyr')
count(x)

Yields below output.


# Output
  x freq
1 1    3
2 2    4
3 3    1
4 4    1
5 6    1
6 7    1

6. Using vctrs Package

Finally use vec_count() function from vctrs package, This function takes the vector as input and returns a data.frame with each unique value from the vector and how many times each value is repeated.


# Using vctrs package
library('vctrs')
vec_count(x)
vec_count(x, sort = "key")

Yields below output.


# Output

> vec_count(x)
  key count
1   2     4
2   1     3
3   7     1
4   4     1
5   6     1
6   3     1

> vec_count(x, sort = "key")
  key count
1   1     3
2   2     4
3   3     1
4   4     1
5   6     1
6   7     1

Conclusion

In this article, you have learned how to get the frequency count of all unique values in the R vector.

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium