I have a huge data frame that contains many time correlated observations of several variables on a few hundred individuals. The individuals each have a unique number in the ID column. I will use the data simulated below, which is structured similarly to my data to ask my question:
set.seed(123)
dat <- data.frame(ID = rep(letters[1:10], each = 10),
time = rep(c(1:10), times = 10),
var1 = rnorm(100))
Note that in the real data, the actual number of observations is different for each ID.
Say there were a few individuals (e.g., IDs: b, e, and g) that I needed to take the observations for and completely "flip" or "reverse" the order of, and still preserve what data is with each time. By this I mean (using individual b as an example) that the first observation in the data frame for individual b would be the data at "time interval" 10 instead of "time interval" 1. In other words the data would look like this:
ID time Var1
a 1
a 2
… …
a 10
b 10
b 9
b 8
… …
b 1
c 1
c 2
c 3
c 4
ect...
What is the safest way to do this and maintain their position in the data frame (i.e., b stays in between a and c ect..)?