Count Unique Values in R (3 Examples) | Frequency of Vector or Column
In this R tutorial you’ll learn how to count the frequency of unique values of a vector or data frame column.
The tutorial looks as follows:
- Example Data
- Example 1: Count Unique Values with table() Function
- Example 2: Modify Output with as.data.frame() Function
- Example 3: Count Unique Values with aggregate() Function
- Video, Further Resources & Summary
If you want to know more about these content blocks, keep reading:
Example Data
In the examples of this R tutorial, we’ll use the following numeric vector:
x <- c(3, 1, 4, 3, 1, 2, 1, 5) # Create example data |
x <- c(3, 1, 4, 3, 1, 2, 1, 5) # Create example data
Our vector contains of 8 elements with a range from 1 to 5. Some of the values occur more than once.
Example 1: Count Unique Values with table() Function
If we want to count the number of times each value occurs, we can use the table function as follows:
table(x) # Apply table function # x # 1 2 3 4 5 # 3 1 2 1 1 |
table(x) # Apply table function # x # 1 2 3 4 5 # 3 1 2 1 1
As you can see based on the previously shown RStudio console output, the table function returns a frequency table of our five values. The value 1 occurs 3 times, the value 2 occurs 1 time, the value 3 occurs 2 times and so on…
Example 2: Modify Output with as.data.frame() Function
The output of the table function might be difficult to read. For a different representation of the table output, we can convert our table to a data frame in R.
For this task, we can use the as.data.frame function as shown below:
as.data.frame(table(x)) # Different representation # x Freq # 1 1 3 # 2 2 1 # 3 3 2 # 4 4 1 # 5 5 1 |
as.data.frame(table(x)) # Different representation # x Freq # 1 1 3 # 2 2 1 # 3 3 2 # 4 4 1 # 5 5 1
The new output is a data frame with two columns. The first column is illustrating the values of our vector and the second column shows the frequency of each of these values.
Example 3: Count Unique Values with aggregate() Function
A completely different approach for the counting of unique values in R is provided by the aggregate function in combination with the data.frame, the list, and the length functions.
Have a look at the following R code:
aggregate(data.frame(count = x), # Apply aggregate function list(value = x), length) # value count # 1 1 3 # 2 2 1 # 3 3 2 # 4 4 1 # 5 5 1 |
aggregate(data.frame(count = x), # Apply aggregate function list(value = x), length) # value count # 1 1 3 # 2 2 1 # 3 3 2 # 4 4 1 # 5 5 1
As you can see, we produced a similar output as in Example 2, but this time based on a completely different R syntax.
Video, Further Resources & Summary
Have a look at the following video of my YouTube channel. In the video, I explain the topics of this tutorial:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you may read the other articles of my website:
- aggregate Function in R
- Get Frequency of Elements with Certain Value in Vector
- unique Function in R
- Convert Frequency Table to Data Frame
- The length Function in R
- The R Programming Language
In this R tutorial you learned how to count the number of distinct values in a vector or column. Please let me know in the comments, in case you have any additional comments or questions. Furthermore, don’t forget to subscribe to my email newsletter to get updates on the newest tutorials.
Statistics Globe Newsletter