The following code counts the number of unique IDs per year. My question is: how to count the number of new unique IDs, i.e., IDs that did not appear in previous years?
df %>%
group_by(year) %>%
summarize(count=n_distinct(ID))
For example, I need to create the variable wanted_count below
| Year | ID | count | wanted_count |
|---|---|---|---|
| 2000 | 1 | 3 | 3 |
| 2000 | 2 | 3 | 3 |
| 2000 | 3 | 3 | 3 |
| 2001 | 2 | 2 | 0 |
| 2001 | 3 | 2 | 0 |
| 2002 | 3 | 2 | 1 |
| 2002 | 4 | 2 | 1 |
| 2003 | 4 | 2 | 1 |
| 2003 | 7 | 2 | 1 |
| 2003 | 4 | 2 | 1 |
See data below:
df <- structure(list(Year = c(2000L, 2000L, 2000L, 2001L, 2001L, 2002L,
2002L, 2003L, 2003L, 2003L), ID = c(1L, 2L, 3L, 2L, 3L, 3L, 4L,
4L, 7L, 4L), count = c(3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), wanted_count = c(3L, 3L, 3L, 0L, 0L, 1L, 1L, 1L, 1L, 1L)), class = "data.frame", row.names = c(NA,
-10L))