0

New to R, I have multiple dataframes in R that I want to combine into a new dataframe. I searched on stackoverflow but have not found an adequate solution. My sample data looks something like this:

df1 <- data.frame(A1 = 10:19, B1 = 40:49, C1 = 70:79)
df2 <- data.frame(A2 = 20:29, B2 = 50:59, C2 = 80:89)
df3 <- data.frame(A3 = 30:39, B3 = 60:69, C3 = 90:99)

df.list <- list(df1, df2, df3)

I want to combine the dataframes column by column, meaning R should take the first column of each dataframe, then the second and third etc. and combine it into a new dataframe. So far I have not managed to find an elegant solution to this problem.

The final result should look like this:

df_final
   A1 A2 A3 B1 B2 B3 C1 C2 C3
1  10 20 30 40 50 60 70 80 90
2  11 21 31 41 51 61 71 81 91
3  12 22 32 42 52 62 72 82 92
4  13 23 33 43 53 63 73 83 93
5  14 24 34 44 54 64 74 84 94
6  15 25 35 45 55 65 75 85 95
7  16 26 36 46 56 66 76 86 96
8  17 27 37 47 57 67 77 87 97
9  18 28 38 48 58 68 78 88 98
10 19 29 39 49 59 69 79 89 99

Any help is greatly appreciated. Thanks in advance!


EDIT:

Sorry if my question was not precise enough. I will try to make my point clear: My problem is to find a command or an easy workaround to make a new dataframe that combines column after column from each dataframe. This means I want to first select column 1 from each dataframe, then column 2 etc. and combine them into a new dataframe.

Simple merging of the dataframes followed by ordering (according to column name) does not help. It would mix up my current order that I would like to keep.

Theo
  • 1
  • 1
  • 2
    possible duplicate of https://stackoverflow.com/q/15124590/4137985 (`Reduce(cbind, df.list)` and then reorder) – Cath Jul 09 '18 at 10:02
  • thank you for your comment @Cath. however, it don't solve my problem as I would like to combine the DFs column after column (without reordering columns afterwards). I made an edit in the original post for clarification. – Theo Jul 09 '18 at 14:20
  • combining column by column or combining and reordering the columns give exact same output in the end – Cath Jul 09 '18 at 14:21
  • To be more specific : `newdf <- do.call(cbind, df.list) ; newdf <- newdf[, order(names(newdf))]` – Cath Jul 09 '18 at 14:28

0 Answers0