Chetan Arvind Patil
Chetan Arvind Patil

Reputation: 866

R Programming - Combine Data Columnwise

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.

Upvotes: 0

Views: 80

Answers (2)

Pierre Lapointe
Pierre Lapointe

Reputation: 16277

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

Upvotes: 1

An economist
An economist

Reputation: 1311

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])
}

Upvotes: 2

Related Questions