0

although I have found a lot of ways to calculate the sum of a variable by group, all the approaches end up creating a new data set which aggregates the double cases.

To be more precise, if I have a data frame:

 id  year    
 1   2010
 1   2015
 1   2017
 2   2011
 2   2017
 3   2015

and I want to count the number of times I have the same ID by the different years, there are a lot of ways (using aggregate, tapply, dplyr, sqldf etc) which use a "group by" kind of functionality that in the end will give something like:

 id   count
 1      3
 2      2
 3      1

I haven't managed to find a way to calculate the same thing but keep my original data frame, in order to obtain:

 id  year   count  
 1   2010     3
 1   2015     3
 1   2017     3
 2   2011     2
 2   2017     2
 3   2015     1

and therefore do not aggregate my double cases. Has somebody already figured out? Thank you in advance

am.nik
  • 5
  • 5

0 Answers0