Here we can see some example data:
id <- c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5)
t <- c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)
md <- c(NA,NA,NA,1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4)
dat <- data.frame(id, t, md)
dat
> dat
id t md
1 1 1 NA
2 1 2 NA
3 1 3 NA
4 2 1 1
5 2 2 1
6 2 3 1
7 3 1 1
8 3 2 1
9 3 3 1
10 4 1 1
11 4 2 1
12 4 3 1
13 5 1 4
14 5 2 4
15 5 3 4
We have a person id (id), three different points in time (t) and a mother id (md). One can see that one person has no mother in the sample data (person with ID "1"). The person with ID "1" is the mother of the persons with ID's 2-4. At the same time, the person with ID "4" is the mother of the person with ID "5".
Now to the question: Going on from this dataset, how to create new variables for the child ID's? The result should look like this:
> dat
id t md kd1 kd2 kd3 kd4
1 1 1 NA 2 3 4 NA
2 1 2 NA 2 3 4 NA
3 1 3 NA 2 3 4 NA
4 2 1 1 NA NA NA NA
5 2 2 1 NA NA NA NA
6 2 3 1 NA NA NA NA
7 3 1 1 NA NA NA NA
8 3 2 1 NA NA NA NA
9 3 3 1 NA NA NA NA
10 4 1 1 5 NA NA NA
11 4 2 1 5 NA NA NA
12 4 3 1 5 NA NA NA
13 5 1 4 NA NA NA NA
14 5 2 4 NA NA NA NA
15 5 3 4 NA NA NA NA
As wen can see, as many new variables are to be created as children are identifiable. For each identifiable child, the person-specific ID should be recorded in a separate variable. How could this be realized using dplyr?
Note: This is only example data, the real data set is much more complex. Therefore, a flexible approach would be desirable.