I have a data set like this
set.seed(1)
df <- data.frame(ID = rep(1:4, each = 3),
x = c(1,2,3,2,3,4,1,2,3,3,4,5),
V1 = rnorm(12))
> df
ID x V1
1 1 1 -0.6264538
2 1 2 0.1836433
3 1 3 -0.8356286
4 2 2 1.5952808
5 2 3 0.3295078
6 2 4 -0.8204684
7 3 1 0.4874291
8 3 2 0.7383247
9 3 3 0.5757814
10 4 3 -0.3053884
11 4 4 1.5117812
12 4 5 0.3898432
this example contains 4 individuals, defined by ID
.
Each individual has an observation period x
. For example ID 1 is observed at time points 1,2,3.
In this example I have 2 observations at time point 1 (ID 1 and ID 3), and 3 observations at time point 2 (IDs 1,2,3)
I now want a bootstrapped (sample with replacement) data set that contains the same number of observations at each time point.
In this example the data set could look like this:
> df
ID x V1
1 1 1 -0.6264538
1 1 1 -0.6264538
2 1 2 0.1836433
2 1 2 0.1836433
3 1 3 -0.8356286
4 2 2 1.5952808
5 2 3 0.3295078
6 2 4 -0.8204684
6 2 4 -0.8204684
7 3 1 0.4874291
7 3 1 0.4874291
8 3 2 0.7383247
9 3 3 0.5757814
10 4 3 -0.3053884
11 4 4 1.5117812
11 4 4 1.5117812
12 4 5 0.3898432
12 4 5 0.3898432
12 4 5 0.3898432
12 4 5 0.3898432
this data set now has 4 observations at each time point.