0

I have two data sets, and both have same dimensions, and want to combine them such that 1st column of second data set is stacked next to 1st column of first data set, and so on.

Consider below example, which is the expected output. Here, v1 is coming from data set 1, and v2 is coming from data set 2. I also want to keep the column header as it is.

                                 |   v1   |   v2   | 
                                 |:------:|:------:|
                                 | -0.71  | -0.71  |
                                 | -0.71  | -0.71  |
                                 | -0.71  | -0.71  |
                                 | -0.71  | -0.71  |
                                 | -0.71  | -0.71  |
                                 | -0.71  | -0.71  |

I tried cbind() and data.frame(), but both led to second data being added after the full first data set, not column after column.

 -> dim(firstDataSet)
    100 200
 -> dim(secondDataSet)
    100 200
 -> finalDataSet_cbind <- cbind(firstDataSet, secondDataSet)
 -> dim(finalDataSet_cbind)
    100 400
 -> finalDataSet_dframe <- data.frame(firstDataSet, secondDataSet)
 -> dim(finalDataSet_dframe)
    100 400

Please suggest correct and better ways to achieve this, thanks.


UPDATE: Response to possible duplicate flag to this question:

That answer didn't work out for me. The data I get after following the solution, didn't result in what I want, and was similar to final output I get with cbind() approach explained above.

The first answer given, works out for me, but with a small issue of new column name assigned to each column, instead of keeping the original column headers.

Also, I don't have enough reputation to add comment to the accepted answer.

Chetan Arvind Patil
  • 854
  • 1
  • 11
  • 31
  • 2
    Possible duplicate of [Merge & Interleave dataframes in r](https://stackoverflow.com/questions/24576548/merge-interleave-dataframes-in-r) – Mr. Bugle Jun 30 '17 at 16:20

2 Answers2

2

Probably not the most efficient solution with for loop, but works

data1 <- cbind(1:10,11:20, 21:30)
data2 <- cbind(1:10,11:20, 21:30)

combined <- NULL
for(i in 1:ncol(data1)){
  combined <-  cbind(combined, data1[,i], data2[,i])
}
An economist
  • 1,301
  • 1
  • 15
  • 35
  • Thanks. It works, I just need to figure out how to ensure the column header is not changed. With your approach, the column names are reassigned as v1,v2...vn, but the data is stacked as I wanted it to be. – Chetan Arvind Patil Jun 30 '17 at 16:38
  • Yes, but I also want to keep the column name as the original data, I did ask this in question above. Will mark it after I figure it out. If you have suggestions, please share. – Chetan Arvind Patil Jun 30 '17 at 17:34
  • Question was never changed, @Aneconomist. The edited version you see was to add response for duplicate flag. History is there, go ahead and have a look. Also, I was trying to upvote your answer (as the one by PLapointe is accurate, and yours is correct too, but misses one thing), but I don't have enough reputation to do that. Link to history: https://stackoverflow.com/posts/44850840/revisions – Chetan Arvind Patil Jun 30 '17 at 22:49
1

To fix the column name requirement, you could do this. Basically, you first cbind, then you create an index in the right order. Using that index, you also create a vector of correct column names. You then index the order of the columns, and add the column names.

df1 <- df2 <- data.frame(v1=1:10,v2=11:20, v3=21:30)
final <- cbind(df1,df2)

indexed <- rep(1:ncol(df1), each = 2) + (0:1) * ncol(df1)
new_colnames <- colnames(final)[indexed]
final_ordered <- final[indexed]
colnames(final_ordered) <- new_colnames

   v1 v1 v2 v2 v3 v3
1   1  1 11 11 21 21
2   2  2 12 12 22 22
3   3  3 13 13 23 23
4   4  4 14 14 24 24
5   5  5 15 15 25 25
6   6  6 16 16 26 26
7   7  7 17 17 27 27
8   8  8 18 18 28 28
9   9  9 19 19 29 29
10 10 10 20 20 30 30
Pierre Lapointe
  • 16,017
  • 2
  • 43
  • 56