I have a dataset with the following structure
study_id encounter_date
1 01/01/2010
2 01/01/2010
2 01/01/2011
3 01/01/2010
3 01/01/2011
3 01/01/2012
And I would like to create a new variable with number of unique encounters for each study_id, as follows (using R):
study_id encounter_date encounter
1 01/01/2010 1
2 01/01/2010 1
2 01/01/2011 2
3 01/01/2010 1
3 01/01/2011 2
3 01/01/2012 3
I have tried using sum(n_distinct(encounter_date)
without success.
Advice?