I have three equal-sized data frames that I want to merge in a particular order so that I can then use write.table to make a formatted text file for use in another program.
I want this:
dataframeA_col1 dataframeB_col1 dataframeC_col1 dataframeA_col2 dataframeB_col2 dataframeC_col2 etc…
Not this:
dataframeA_col1 dataframeA_col2 dataframeB_col1 dataframeB_col2
I started to write the loop below which works but only gives me the first 3 columns. How can I fix this loop, or is there a better approach?
temp <- c()
for(i in length(dataframeA)){
temp <- cbind(temp, dataframeA[,i], dataframeB[,i], dataframeC[,i])
i <- i+1
}
Sure, here’s a loop-less way:
where
colSeqis your desired column sequence:and now use this
colSeqto rearrange the columns ofcbind(dfA,dfB,dfC):