merge(df, aggregate(Deaths ~ Group, df, FUN = function(x) sum(x == 0)), by = "Group", suffixes = c("", "counties.without"))
Group State County Deaths Deathscounties.without
1 1 Nicaragua County A 0 2
2 1 Nicaragua County B 13 2
3 1 Nicaragua County C 0 2
4 2 Mexico County D 0 3
5 2 Mexico County F 0 3
6 2 Mexico County E 0 3
Данные :
df <- structure(list(Group = c(1L, 1L, 1L, 2L, 2L, 2L), State = c("Nicaragua",
"Nicaragua", "Nicaragua", "Mexico", "Mexico", "Mexico"), County = c("County A",
"County B", "County C", "County D", "County F", "County E"),
Deaths = c(0L, 13L, 0L, 0L, 0L, 0L)), row.names = c(NA, -6L
), class = "data.frame")