# Counterpart to Microsoft Excel’s COUNTIF in R (Example Code)

In this tutorial you’ll learn how to realize Excel’s COUNTIF function in R programming.

## Creation of Exemplifying Data

set.seed(3534598) # Constructing example data my_data <- sample(1:5, 20, replace = TRUE) my_data # Printing example data in RStudio console # [1] 5 3 3 4 4 5 4 3 4 4 3 3 5 5 5 2 4 5 5 5 |

set.seed(3534598) # Constructing example data my_data <- sample(1:5, 20, replace = TRUE) my_data # Printing example data in RStudio console # [1] 5 3 3 4 4 5 4 3 4 4 3 3 5 5 5 2 4 5 5 5

## Example: R Programming Counterpart to COUNTIF in Excel

sum(my_data == 2) # COUNTIF in R # [1] 4 |

sum(my_data == 2) # COUNTIF in R # [1] 4