Count Occurrences of Value in Data Frame in R (2 Examples)
This tutorial explains how to count the number of times a certain entry occurrs in a data frame in the R programming language.
The post looks as follows:
Let’s take a look at some R codes in action.
Creating Example Data
I’ll use the following data as a basis for this R programming language tutorial:
data <- data.frame(x1 = letters[1:5], # Create example data frame x2 = c("a", "b", "b", "a", "b"), x3 = "b") data # Print example data frame
Table 1 shows the structure of our exemplifying data: It is made up of five data points and three character columns.
Example 1: Count Certain Value in One Column of Data Frame
In this example, I’ll demonstrate how to get the number of times a particular entry appears in a specific variable of a data frame.
For this task, we can apply the sum function to a logical condition as shown below:
sum(data$x2 == "b") # Count in one column # [1] 3
The RStudio console has returned the value 3 after executing the previous R code, i.e. the character “b” occurs three times in the column x2.
Example 2: Count Certain Value in Entire Data Frame
This example explains how to count a certain value in all columns of a data frame in R.
Once again, we can use the sum function to accomplish this. However, this time we are not specifying a certain column (as we did in Example 1):
sum(data == "b") # Count in all columns # [1] 9
As you can see, the character “b” is contained nine times in our example data set.
Video, Further Resources & Summary
If you need further details on the R programming code of this tutorial, you might want to watch the following video that I have published on my YouTube channel. In the video, I’m explaining the examples of this article.
Besides that, you may want to read some related RStudio articles on my website. Please find a selection of related tutorials about related topics such as numeric values, data inspection, character strings, and vectors below:
- Replace Particular Value in Data Frame in R
- Count Number of Occurrences of Certain Character in String
- Get Value of Data Element without Name or Index
- Find Index of Maximum & Minimum Value of Vector & Data Frame Row
- R Programming Tutorials
To summarize: In this article, I have explained how to count a certain entry occurs in a data frame in the R programming language. Don’t hesitate to let me know in the comments section below, in case you have additional questions.
10 Comments. Leave new
Many thanks.
You are very welcome Aura, glad you like it!
thanks alot👌👌👏
Thanks a lot Zahra, glad you found it helpful!
the problem with using sum(data == “b”) is that it will return NA instead of 0 if that value does not exist in the data. This is a real problem if you assume it return 0 and try to make some arithmetic calculation, especially when you write a function.
Hello Thuy,
Are you sure that it is the case? Check the R script below.
It returns 0 for the character “k”. I hope I got your point correctly. If not, please let me know.
Regards,
Cansu
Like Thuy Scanlon, I also got the “NA” problem.
Seems like the source of the problem is that if you sum anything with NAs present, the result will be NA, unless you handle it properly. The solution if you have NAs in your data, is to add “, na.rm=T” at the end
example: sum(data == “k”, na.rm=T)
Hope this helps!
Hello Felix,
Thank you for the input, it is appreciated!
Regards,
Cansu
How would you get the value counts and percentages of all the variables in the example data frame?
Hello Dan,
Would something like this be helpful? Please see below.
Best,
Cansu