I'm trying to have R go through a loop within ID groups, and within each group:
- find the max year;
- count the number of prior observations (by year) that have a code other than the code corresponding to the current row;
- disregard observations that appear in the same year.
I would very much appreciate ideas how to perform this loop within each ID group
group
ID CODE Year Desired output
1 SCA 1999 0
1 SCA 2005 0
1 SAM 2006 2
1 SEA 2006 2
2 SCA 2007 0
3 WE 1986 0
3 WE 1995 0
3 NE 1996 2
3 NAM 2000 3
3 WE 2000 1
3 SE 2001 5
3 NAM 2003 5
3 SAM 2004 7
3 NAM 2006 6
- find the max year;
- count the number of prior observations (by year) that have a code other than the code corresponding to the current row;
- disregard observations that appear in the same year.
I would very much appreciate ideas how to perform this loop within each ID group
group
ID CODE Year Desired output
1 SCA 1999 0
1 SCA 2005 0
1 SAM 2006 2
1 SEA 2006 2
2 SCA 2007 0
3 WE 1986 0
3 WE 1995 0
3 NE 1996 2
3 NAM 2000 3
3 WE 2000 1
3 SE 2001 5
3 NAM 2003 5
3 SAM 2004 7
3 NAM 2006 6
Last edited: