# Re-sampling with replacement a longitudinal dataset in R

I have a large dataset from a study I conducted in which I'm attempting, for pedagogical reasons, to use for a class. However, due to IRB constraints, I cannot use the real data.

Instead, I would like to re-sample the entire dataset while attempting to keep the original structure of the distributions (i.e., I do not want to artificially manipulate the data into forced normalcy).

However, anytime I use any type of monte carlo or bootstrap method (e.g., sample()), all it seems to do is just re-order the data. Further, due to the nested, repeated measures of the data I'm having a difficult time truly simulating it. Rather, any successful method I've used results in subjects whose repeated values are different when they should be the same.

I've posted an example of the structure of the data below.

You'll notice that the subjects are nested into groups, where multiple measurement days (3 in total), and each measurement day has three different measurement sessions.

Variables 1 - 3 are the same, as they were measured in the beginning of the study and therefore are not repeated (the data is in long-form).

Variable 4 is a repeated measurement, however, is repeated through each measurement day and session.

Can anyone provide a suggestion as to how I can completely re-sample this with replacement such that it maintains its original structure?

I have followed instruction from: https://stackoverflow.com/questions/38466788/r-coding-bootstrap-a-dataset-with-repeated-measures

Which suggested the following code: data[sample(x = nrow(data), size = nrow(data), replace = TRUE), ]

would work, but as mentioned earlier, this only re-orders the data and does not actually replace the data.

n=4