0

I'm currently trying to vertically stack three data frames together in R so I can keep my data clean. The problem is that the ordering of each of the frames is different and I don't want R to reorder each column to match the order of the columns in the first data frame. The data frames look something like this:

df.1
        X1   X2   X3   X4
NA       A    B    C    D
200301   2    4    5    6
200302   4    5    8    9

df.2
        X3   X1   X3   X2     
NA       C    A    D    B
200401   3    1    5    7
200402   2    9    6    4

df.3
        X4   X3   X2   X1     
NA       D    C    B    A
200501   5    4    5    6
200502   8    2    3    8

And I want them to join vertically such that they look like this, obviously without the column labels in between them. I am essentially trying to do the same thing as just copying and pasting the dataframes one underneath each other.

df.4
        X1   X2   X3   X4    
NA       A    B    C    D
200301   2    4    5    6
200302   4    5    8    9
        X3   X1   X3   X2             
NA       C    A    D    B
200401   3    1    5    7
200402   2    9    6    4
        X4   X3   X2   X1
NA       D    C    B    A
200501   5    4    5    6
200502   8    2    3    8

Is there any possible way to achieve this? I've had a fair look and can't seem to find anything that would do this for me. Appreciate any and all help!

cbayntun
  • 11
  • 3
  • `do.call(rbind,list(df.1, df.2, df.3))`, or `dplyr::bind_rows(df.1, df.2, df.3)`. – moodymudskipper Oct 21 '18 at 00:08
  • 1
    @Moody_Mudskipper `bind_rows` would bind the rows based on column names, which may not be what the OP wants. – www Oct 21 '18 at 00:09
  • @Moody_Mudskipper I take my comment back. After the OP's update, I think your solution works. – www Oct 21 '18 at 00:12
  • and I had taken it back where i should have left it :).OP you columns are sorted identically for all data.frames here – moodymudskipper Oct 21 '18 at 00:13
  • @OP if for some reason my 1st comment + the link doesn't answer your question, edit to be explicit about it and ping me and I'll reopen – moodymudskipper Oct 21 '18 at 00:16
  • Apologies guys, I have just updated the questions now, I don't want the columns to move around as part of the bind - if that makes sense? I am essentially trying to do the equivalent of copy and pasting chunks of data onto one sheet in Excel. I don't want the values in the columns of df2 and df3 to move around and be matched with the X column values in df1 (i.e. I want the columns to stay right where they are, and just paste them onto the bottom of the df above it.) – cbayntun Oct 21 '18 at 02:36
  • @Moody_Mudskipper, sorry forgot to ping you in my reply – cbayntun Oct 21 '18 at 02:46
  • alright so it's as www said :), reopening – moodymudskipper Oct 21 '18 at 09:51

1 Answers1

0

NA as row names are not supported so I added one column.

The idea is to rename the columns first and then apply do.call and rbind :

dfs <- list(df.1, df.2, df.3)

dfs <- lapply(dfs, setNames, paste0("V",1:ncol(dfs[[1]])))
res <- do.call(rbind, dfs)
res
#       V1 V2 V3 V4 V5
# 1     NA  A  B  C  D
# 2 200301  2  4  5  6
# 3 200302  4  5  8  9
# 4     NA  C  A  D  B
# 5 200401  3  1  5  7
# 6 200402  2  9  6  4
# 7     NA  D  C  B  A
# 8 200501  5  4  5  6
# 9 200502  8  2  3  8

data

df.1 <- read.table(header=TRUE,stringsAsFactors=FALSE,text="X0 X1   X2   X3   X4
NA       A    B    C    D
200301   2    4    5    6
200302   4    5    8    9")

df.2 <- read.table(header=TRUE,stringsAsFactors=FALSE,text="X0 X3   X1   X3   X2     
NA       C    A    D    B
200401   3    1    5    7
200402   2    9    6    4")

df.3 <- read.table(header=TRUE,stringsAsFactors=FALSE,text="X0 X4   X3   X2   X1     
NA       D    C    B    A
200501   5    4    5    6
200502   8    2    3    8")
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167