Merry Christmas
I would like to split a long dataframe. The dataframe looks like this
x<-c('0:00:00', '0:30:00', '1:00:00', '1:30:00', '2:00:00', '2:30:00', '3:00:00',
'0:00:00', '0:30:00', '1:00:00', '1:30:00', '2:00:00', '2:30:00', '3:00:00',
'3:30:00', '4:00:00','0:00:00', '0:30:00', '1:00:00', '1:30:00', '2:00:00',
'2:30:00', '3:00:00', '0:00:00', '0:30:00', '1:00:00', '1:30:00', '2:00:00',
'2:30:00', '3:00:00' , '3:30:00', '4:00:00')
y=seq(1:32)
data1=data.frame(x,y)
i want to split in such a way that the output looks like
0:00:00 1 8 17 24
0:30:00 2 9 18 25
1:00:00 3 10 19 26
1:30:00 4 11 20 27
2:00:00 5 12 21 28
2:30:00 6 13 22 29
3:00:00 7 14 23 30
3:30:00 NA 15 NA 31
4:00:00 NA 16 NA 32
any ideas or functions that i look into for doing this? I tried using split function, but could not get it done.
Thanks a lot for your help and time.
The below solution by Matthew works best. However if i increase the cycle time for x
x<-c('0:00:00', '0:30:00', '1:00:00', '1:30:00', '2:00:00', '2:30:00', '3:00:00', '3:30:00',
'4:00:00', '4:30:00', '5:00:00', '5:30:00', '6:00:00', '6:30:00', '7:00:00',
'7:30:00','8:00:00', '8:30:00', '9:00:00', '9:30:00', '10:00:00', '10:30:00',
'11:00:00','11:30:00','0:00:00', '0:30:00', '1:00:00', '1:30:00', '2:00:00', '2:30:00',
'3:00:00', '3:30:00', '4:00:00', '4:30:00', '5:00:00', '5:30:00', '6:00:00', '6:30:00',
'7:00:00', '7:30:00','8:00:00', '8:30:00', '9:00:00', '9:30:00', '10:00:00', '10:30:00',
'11:00:00','11:30:00', '12:00:00', '12:30:00', '13:00:00', '13:30:00')
and use the same code, i get the following error:
Error in match.names(clabs, names(xi)) : names do not match previous names
Cheers,
Swagath
If we can assume that each new cycle starts at
0:00:00and that each new cycle will always include a0:00:00, then we can easily usereshape()after creating a “time” variable usingcumsum().