I have a data frame with multiple columns and multiple rows, which looks like:
V1 V2 V3 V4 V5 V6
1 1 2 3 13 14 15
2 4 5 6 16 NA NA
3 7 8 9 19 20 21
4 10 11 12 22 23 24
And I want to reshape it into:
V1 V2 V3
1 1 2 3
2 4 5 6
3 7 8 9
4 10 11 12
5 13 14 15
6 16 NA NA
7 19 20 21
8 22 23 24
In the original data.frame, keep every 3 columns as a group, such that (V1
, V2
, V3
) is group1, (V4
, V5
, V6
) is group2, etc. Then move group2 - without the order of values changing - to the end of group1, and move group3 to the end of group2.
I tried:
as.data.frame(matrix(unlist(mydata, use.names=FALSE), ncol=3, byrow=TRUE))
but have the value order problem.
How can I get my desired data structure?