I have the following dataset:
df<-structure(list(IDFAM = c("2010 7599 2996 1", "2010 7599 3071 1",
"2010 7599 3071 1", "2010 7599 3660 1", "2010 7599 4736 1", "2010 7599 6235 1",
"2010 7599 6299 1", "2010 7599 9903 1", "2010 7599 11013 1",
"2010 7599 11778 1", "2010 7599 11778 1", "2010 7599 12248 1",
"2010 7599 13127 1", "2010 7599 14261 1", "2010 7599 16280 1",
"2010 7599 16280 1", "2010 7599 16280 1", "2010 7599 16280 1",
"2010 7599 16280 1", "2010 7599 17382 1"), AGED = c(45L, 47L,
24L, 46L, 46L, 44L, 43L, 43L, 43L, 16L, 43L, 46L, 44L, 47L, 43L,
16L, 20L, 18L, 18L, 43L)), .Names = c("IDFAM", "AGED"), row.names = c("5614",
"5748", "5753", "6864", "8894", "11761", "11884", "18738", "20896",
"22351", "22353", "23267", "24939", "27072", "30946", "30947",
"30949", "30950", "30952", "33034"), class = "data.frame")
I would like to assign an ID to each observation having the same IDFAM
value ranging from 1 to n, where n is the number of observations with the same value of IDFAM
. This would result in the following table:
IDFAM AGED ID
2010 7599 2996 1 45 1
2010 7599 3071 1 47 1
2010 7599 3071 1 24 2
2010 7599 3660 1 46 1
2010 7599 4736 1 46 1
2010 7599 6235 1 44 1
2010 7599 6299 1 43 1
2010 7599 9903 1 43 1
2010 7599 11013 1 43 1
2010 7599 11778 1 16 1
2010 7599 11778 1 43 2
2010 7599 12248 1 46 1
2010 7599 13127 1 44 1
2010 7599 14261 1 47 1
2010 7599 16280 1 43 1
2010 7599 16280 1 16 2
2010 7599 16280 1 20 3
2010 7599 16280 1 18 4
2010 7599 16280 1 18 5
2010 7599 17382 1 43 1
How can I do this ? Thanks.