Count observatons by group with dplyr
Solution 1:
To following produces your expected output:
data.el %>%
group_by(node) %>%
mutate(n_yy = sum(yy)) %>%
select(time, yy, node, n_yy)
If yy
is not just a binary variable, then I would suggest:
data.el %>%
group_by(node) %>%
mutate(n_yy = sum(ifelse(yy != 0, 1, 0))) %>% # make binary
select(time, yy, node, n_yy)