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 data frame count occurrences value data frame r

 

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:

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


10 Comments. Leave new

  • Many thanks.

    Reply
  • thanks alot👌👌👏

    Reply
  • 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.

    Reply
    • Hello Thuy,

      Are you sure that it is the case? Check the R script below.

      data <- data.frame(x1 = letters[1:5],    # Create example data frame
                         x2 = c("a", "b", "b", "a", "b"),
                         x3 = "b")
      data    
       
      sum(data$x2 == "b")                      # Count in one column
      # [1] 3
       
      sum(data == "k")    
      # [1] 0

      It returns 0 for the character “k”. I hope I got your point correctly. If not, please let me know.

      Regards,
      Cansu

      Reply
  • 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!

    Reply
  • How would you get the value counts and percentages of all the variables in the example data frame?

    Reply
    • Hello Dan,

      Would something like this be helpful? Please see below.

      df <- data.frame(
        A = c('yes', 'no', 'yes', 'yes', 'no'),
        B = c('blue', 'red', 'blue', 'green', 'green'),
        C = c(1, 1, 2, 2, 1)
      )
       
      counts <- table(df)
      counts 
      # , , C = 1
      # 
      #       B
      # A     blue green red
      # no     0     1   1
      # yes    1     0   0
      # 
      # , , C = 2
      #  
      #       B
      # A     blue green red
      # no     0     0   0
      # yes    1     1   0
       
      prop.table(counts)*100
      # , , C = 1
      # 
      #       B
      # A     blue green red
      # no     0    20  20
      # yes   20     0   0
      # 
      # , , C = 2
      # 
      #       B
      # A     blue green red
      # no     0     0   0
      # yes   20    20   0

      Best,
      Cansu

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top