According to this previous post, I can add a column with a count of occurrences in the past year in the following way:
df[, boundary := date - 365]
df[, counts := df[df, .N, on = .(id, date < date, date > boundary), by = .EACHI]$N]
This works fine for me. However, I want to do this by counting only the number of occurrences where another column has a specific value. For example, given a dataset like this
id type date
ny 0 2021-09-27
ny 0 2021-09-09
ny 1 2021-08-01
ny 1 2021-07-07
ch 0 2020-04-01
ch 1 2020-03-01
ch 0 2020-02-01
I want to count only the number of rows where type = 1
. How can I amend the function above to do this? I tried something like this, but it doesn't work:
df[, counts := df[df, .N(type = 1), on = .(id, date < date, date > boundary), by = .EACHI]$N]
EDIT: Expected output for the above dataset would be:
id type date counts
ny 0 2021-09-27 2
ny 0 2021-09-09 2
ny 1 2021-08-01 1
ny 1 2021-07-07 0
ch 0 2020-04-01 1
ch 1 2020-03-01 0
ch 0 2020-02-01 0