I have this dataset
ID | days
1 | 2
1 | 31
1 | 22
2 | 24
2 | 19
And I want to create a new column with the count of the same ID but just count with days are > 20,
ID | days | Count
1 | 2 | 2
1 | 31 | 2
1 | 22 | 2
2 | 24 | 1
2 | 19 | 1
I have tried this:
dataset1 = dataset1[ days > 20,count_IDs :=.N, by = 'ID' ]
but that only assigns a value to those rows where days > 20, I want to add that value in all rows of the same ID. If this can be resolved with data.frame it would be great