
Simple illustration, 
 
> df3 <- data.frame(id=c(3,2,1,4), age=c(40,50,60,50), dose1=c(1,2,1,2), dose2=c(2,1,2,1), dose4=c(3,3,3,3))> df3  id age dose1 dose2 dose41  3  40     1     2     32  2  50     2     1     33  1  60     1     2     34  4  50     2     1     3> melt.data.frame(df3, id.var=1:2, na.rm=T)   id age variable value1   3  40    dose1     12   2  50    dose1     23   1  60    dose1     14   4  50    dose1     25   3  40    dose2     26   2  50    dose2     17   1  60    dose2     28   4  50    dose2     19   3  40    dose4     310  2  50    dose4     311  1  60    dose4     312  4  50    dose4     3
 
My objective is to get the below such that the "long" form is in the same order as the "id" column of df3. 
   id age variable value1   3  40    dose1     12   3  40    dose2     2
3   3  40    dose4     3
4   2  50    dose1     25   2  50    dose2     1
6   2  50    dose4     3
7   1  60    dose1     18   1  60    dose2     2
9   1  60    dose4     3
10   4  50    dose1     211   4  50    dose2     112  4  50    dose4     3
_________________________________________________________________


	[[alternative HTML version deleted]]

