I have a wide data set that I want to rearrange. It looks something like this:
id <- c(100,101,102)
variablea_1 <- c(1,1,1)
variableb_1 <- c(1,1,1)
variablec_1 <- c(1,1,1)
varibaled_1 <- c(1,1,1)
variablea_2 <- c(1,1,1)
variableb_2 <- c(1,1,1)
variablec_2 <- c(1,1,1)
varibaled_2 <- c(1,1,1)
variablea_3 <- c(1,1,1)
variableb_3 <- c(1,1,1)
variablec_3 <- c(1,1,1)
varibaled_3 <- c(1,1,1)
Data <- data.frame(patientid=patientid, variablea_1= variablea_1, variableb_1 =variableb_1,variablec_1 =variablec_1, varibaled_1 =varibaled_1, variablea_2 =variablea_2,
variableb_2 = variableb_2, variablec_2 = variablec_2, varibaled_2 = varibaled_2, variablea_3 = variablea_3, variableb_3 =variableb_3, variablec_3 <- variablec_3, varibaled_3 = varibaled_3)```
The data itself is unimportant. Looking for a way rearrange columns so they are in order of variables grouped that are the same and then proceed by time point (In my actual dataset I have 80 variables at three timepoints so 240 columns total) So desired output would look like this:
Data <- data.frame(patientid=patientid, variablea_1= variablea_1, variablea_2 =variablea_2,variablea_3 = variablea_3,variableb_1 =variableb_1, variableb_2 = variableb_2,variableb_3 =variableb_3,variablec_1 =variablec_1, variablec_2 = variablec_2,variablec_3 <- variablec_3, varibaled_1 =varibaled_1, varibaled_2 = varibaled_2, varibaled_3 = varibaled_3)```