This will do the trick
library(dplyr)
test <- data.table::data.table(name = c(rep(1,20), rep(2,20), rep(3,20)), type = c(rep("apple",10), rep("pear",10), rep("apple",15), rep("pear",5), rep("pear",20)))
test %>%
group_by(name) %>%
summarise(diff = sum(type == "apple") - sum(type == "pear"))