To group by and count with a condition in R, you can use functions from dplyr package.
The following method shows how you can do it with syntax.
Method: Use dplyr Package
df %>%
group_by(column1) %>%
summarise(count = sum(condition on column2))
The following example shows how to group by and count with condition in R using dplyr package.
Using dplyr Package
Let’s see how we can use dplyr package in R:
# Create dataframe
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"))
# Group by and count values based on condition
df %>%
group_by(Machine_name) %>%
summarise(count = sum(Status == "OK"))
Output:
Machine_name count
<chr> <int>
1 A 3
2 B 1
3 C 2
4 D 1
5 E 0
6 F 0
7 G 0
8 H 2
Here the output shows count of repeated values in Machine_name column of dataframe.