COUNTIF Function in R

COUNTIF Function in R, As we know if we want to count the length of the vector we can make use of the length function. In case you want to count only the number of rows or columns that meet some criteria, Yes we can do it easily.

COUNTIF Function in R

Basic syntax:

sum(df$column == value, na.rm=TRUE)

Let’s create a data frame for the COUNTIF Function in R.

data <- data.frame(Product=c("A", "A", "B", "B", "A"),
                   score=c(10, 12, 8, 17, 12),
                   value=c(18, 5, 15, 19, 10))
data
    Product score value
1       A    10    18
2       A    12     5
3       B     8    15
4       B    17    19
5       A    12    10

tidyverse in r – Complete Tutorial » Unknown Techniques »

Count Rows Equal to Some Value

Suppose if you want to count the number of rows where the product equal to “B”:

sum(data$Product == 'B')
[1] 2

The following code executes the count, a number of rows where the product is equal to “B” or “A”:

sum(data$ Product == 'A' | data$ Product == 'B')
[1] 5

The following code display how to count the number of rows where the product is not equal to “A”:

sum(data$ Product!= 'A')
[1] 2

Equality of Variances in R-Homogeneity test-Quick Guide »

Count Rows Greater or Equal to Some Value

The following code display the number of rows where the score is greater than 11:

sum(data$score > 11, na.rm=TRUE)
[1] 3

The following code display the number of rows where the value is less than or equal to 10:

sum(data$value <= 10, na.rm=TRUE)
[1] 2

How to find z score in R-Easy Calculation-Quick Guide »

Count Rows Between Two Values

The following code displays the number of rows where the score is between 10 and 18:

sum(data$score > 10 & data$score < 18, na.rm=TRUE)
[1] 3

Note:-You can make use of the %>% operator and count functions or direct count_if function.

All information is important here we collated some pieces of information about newspapers and founders in India »

Subscribe to the Newsletter and COMMENT below!

[newsletter_form type=”minimal”]

You may also like...

Leave a Reply

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

five × two =

finnstats