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"),

# Group by and count values based on condition
df %>%
  group_by(Machine_name) %>%
  summarise(count = sum(Status == "OK"))


  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.