To count the value in column with condition in R, you can use two different ways one is using nrow() function and other is using summarise() function from dplyr package.

The following methods show how you can do it with syntax.

Method 1: Use nrow() Function

nrow(df[df$column_name == value, ])  Method 2: Use summarise() Function df %>% filter(column_name == value) %>% summarise(count = n())  The following examples show how to count value in column with condition in R. ## Count Values in Column with Condition Using nrow() Function in R Let’s see how we can use nrow() function to count value in column based on condition: # Create data frame df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H","A","B","A","C","D","B","E","H"), Pressure=c(12.39,11.25,12.15,13.48,13.78,12.89,12.21,12.58,9.6,8.85,7.89,10.24,12.36,11.45,9.47,8.12), Status=c("OK","Suspect","OK","OK","Suspect","Suspect","Suspect","OK","OK","OK","OK","OK","Suspect","Suspect","Suspect","OK")) # Apply condition on single column of data frame nrow(df[df$Status == 'Suspect', ])


Output:

[1] 7


Here the output shows count for Suspect value in Status column of data frame.

## Count Values in Column with Condition Using smmarise() Function in R

Let’s see how we can use summarise() function to count values in columns with condition in R:

library(dplyr)

# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H","A","B","A","C","D","B","E","H"),
Pressure=c(12.39,11.25,12.15,13.48,13.78,12.89,12.21,12.58,9.6,8.85,7.89,10.24,12.36,11.45,9.47,8.12),
Status=c("OK","Suspect","OK","OK","Suspect","Suspect","Suspect","OK","OK","OK","OK","OK","Suspect","Suspect","Suspect","OK"))

# Count values in Status where Status is "Suspect"
result <- df %>%
filter(Status == "Suspect") %>%
summarise(count = n())

# Show count
print(result)


Output:

  count
1     7


Here the output shows count 7 for Suspect values in Status column of data frame.