I need help in indexing grouped data. In this excample (df1 data), the first 
child had a first immunization at age 2. The second child had the first, second 
and third immunization at age 5,10, and 12, the third child had first and 
second immunization at age 4 and 6 and the fourth child had the first 
immunization at age 2. I have df1 and I need to create df2 with and "ind' 
variable that indicate if the immunization is first, second or third. Note that 
the data is not balanced but is sorted such that the fisrt observation (of an 
individual) is the first immunization. 
 
 
> df1 <- data.frame(id = c(1,2,2,2,3,3,4), age = c(4,5,10, 12, 4,6, 2), dose = 
> c(1.8,1.8,1.6,1.2,1.8,1.6,1.8))
>               
> df2 <- data.frame(id = c(1,2,2,2,3,3,4), age = c(4,5,10, 12, 4,6, 2), 
> ind=c(1,1,2,3,1,2,1), dose = c(1.8,1.8,1.6,1.2,1.8,1.6,1.8))
> 
> df1
  id age dose
1  1   4  1.8
2  2   5  1.8
3  2  10  1.6
4  2  12  1.2
5  3   4  1.8
6  3   6  1.6
7  4   2  1.8
> df2
  id age ind dose
1  1   4   1  1.8
2  2   5   1  1.8
3  2  10   2  1.6
4  2  12   3  1.2
5  3   4   1  1.8
6  3   6   2  1.6
7  4   2   1  1.8
> 


Peter Maclean
Department of Economics
UDSM
        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to