This is the main data analysis problem that Stata does in one step.
Create a wide data frame with data over time (x0) and time-varying data for 2000 and 2005 (x1, x2):
d1 <- data.frame(subject = c("id1", "id2"), x0 = c("male", "female"), x1_2000 = 1:2, x1_2005 = 5:6, x2_2000 = 1:2, x2_2005 = 5:6 )
st
subject x0 x1_2000 x1_2005 x2_2000 x2_2005 1 id1 male 1 5 1 5 2 id2 female 2 6 2 6
I want to shape it like a panel, so the data looks like this:
subject x0 time x1 x2 1 id1 male 2000 1 1 2 id2 female 2000 2 2 3 id1 male 2005 5 5 4 id2 female 2005 6 6
I can do it with reshape st
d2 <-reshape(d1, idvar="subject", varying=list(c("x1_2000","x1_2005"), c("x2_2000","x2_2005")), v.names=c("x1","x2"), times = c(2000,2005), direction = "long", sep= "_")
My main concern is that when you have dozens of variables, this command becomes very long. In stata you can simply enter:
reshape long x1 x2, i(subject) j(year)
Is there such a simple solution in R?
r reshape data-manipulation panel stata
Fred
source share