0

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?

Marcos
  • 1
  • 4
  • Does this answer your question? [Counting unique / distinct values by group in a data frame](https://stackoverflow.com/questions/12840294/counting-unique-distinct-values-by-group-in-a-data-frame) – NelsonGon Feb 17 '20 at 13:51
  • Try(can use `mutate`): `df %>% group_by(study_id) %>% summarise(Uniq=n_distinct(encounter_date))` but `id` 2 should have two unique values, no? Maybe make your definition of unique more explicit. – NelsonGon Feb 17 '20 at 13:53

0 Answers0