I have longitudinal data with multiple measurements on a number of objects over time. The data is in long format with hundreds of variables and cases:
Id Time1 Measurement11 ... Time2 Measurement21 ...
1 50.4 23 ... 52.1 25 ...
2 64.3 30 ... 67.9 35 ...
3 70.1 20 ... 72.3 29 ...
I would like to convert it to wide format for plotting
Id Time Measurement1 ...
1 50.4 23 ...
1 52.1 25 ...
2 64.3 30 ...
2 67.9 35 ...
3 70.1 20 ...
4 72.3 29 ...
I checked resources on gather, melt, reshape, reshape2 but it seems they deal with situations where there are multiple time columns which all contain measurements. So far I have not found a good way to convert the data while maintaing the pairwise dependency on time and measurement.
My current solution is to write something like
attatch(data)
temp1<-bind_cols(Time1,Measurement11)
temp2<-bind_cols(Time2,Measurement21)
wide_format_measurement1<-bind_rows(temp1,temp2,...)
It works since there are only 5 time variables, but it does not seem very efficient. I could use unite to create pairs of data, then use gather, and finally use separate. But this is essentially the same as the bind_cols method. There must be a better way?
dput()
to supply a sample of your data. – OTStats