Conditional count with zero with dplyr

Solution 1:

You may first summarise the data to count number of Tool "A" in each ID and then count the counts.

library(dplyr)

df %>%
  group_by(ID) %>%
  summarise(ToolA = sum(Tool == "A")) %>%
  count(ToolA, name = "count")

# ToolA   count
#  <int> <int>
#1     0     1
#2     1     3
#3     2     1