The title may be a little ambiguous, let me explain what I'm trying to do
This is my data frame:
Person<-c("Andrew","John","Mike")
Age<-c(25,34,21)
ColA_1<-c(1,5,7)
ColA_2<-c(5,0,9)
ColA_3<-c(4,4,1)
ColB_1<-c(16,55,37)
ColB_2<-c(25,14,39)
ColB_3<-c(43,64,31)
df<-data.frame(Person=Person,Age=Age,
ColA_1=ColA_1,
ColA_2=ColA_2,
ColA_3=ColA_3,
ColB_1=ColB_1,
ColB_2=ColB_2,
ColB_3=ColB_3)
What I would like to achieve is a new dataframe in long format, with columns B (ColB_1,ColB_2,ColB_3) beneath columns A, while duplicating value for Person and Age. In SPSS this would be the equivalent of VARSTOCASES. The output data frame woulda have 6 columns: two for Person and Age, three for the collapsed A and B columns and one indicating if the row reffers to A or B data.
I've meddled with melt function from {reshape2} package, but it seems it can only summarize the data into a single column instead of three. My only solution would be create two dataframes, first with A columns, second with B and the rbind() them together. However, I need to achieve this for a very complex data set and I'm hoping for an easier and more elegant solution.
The expected result is:
Person Age Index Col_1 Col_2 Col_3
1 Andrew 25 A 1 5 4
2 John 34 A 5 0 4
3 Mike 21 A 7 9 1
4 Andrew 25 B 16 25 43
5 John 34 B 55 14 64
6 Mike 21 B 37 39 31