Merge two dataframes of the same name keeping all columns

Asked

Viewed 82 times

0

I have two data frames with the same name in the columns, example:

df1 <- read.table(text = "Nom1 Nom2   
  15.1 20.3
  45.5 40.1
  32.1 50.2", header = T)

df2 <- read.table(text = "Nom1 Nom2  
  10.1 90.3
  35.5 80.1
  42.1 50.2", header = T)

I’d like to unite df1 and df2 in order to obtain the following result:

|   Nom1   |  Nom2    |
| 15.1|10.1| 20.3|90.3|
| 45.5|35.5| 40.1|80.1| 
| 32.1|42.1| 50.2|50.2|

i tried already tried with merge and Join functions but none results in my goal

  • merge(df1, df2, by = "row.names")?

  • I’ve tried it that way but what happens is that it joins the dataframes side by side. So: name1 name 2 name 1 name 2, I needed a name 1 name 1 name 2 name 2

3 answers

1

You can do it like this:

library(magrittr)

merge(df1, df2, by = "row.names") %>% # agrega os dados
  .[-1] %>% # remove a coluna Row.names
  .[sort(colnames(.))] # ordena as colunas

#  Nom1.x Nom1.y Nom2.x Nom2.y
#1   15.1   10.1   20.3   90.3
#2   45.5   35.5   40.1   80.1
#3   32.1   42.1   50.2   50.2

0

Can use cbind to put the two data.frames side by side and then sort the columns by name:

df1 <- read.table(
  text = "Nom1 Nom2
          15.1 20.3
          45.5 40.1
          32.1 50.2",
  header = TRUE)

df2 <- read.table(
  text = "Nom1 Nom2
          10.1 90.3
          35.5 80.1
          42.1 50.2",
  header = TRUE)

df3 <- cbind(df1, df2)

df3
#>    Nom1 Nom2 Nom1 Nom2
#> 1: 15.1 20.3 10.1 90.3
#> 2: 45.5 40.1 35.5 80.1
#> 3: 32.1 50.2 42.1 50.2

df3[order(names(df3))]
#>   Nom1 Nom1.1 Nom2 Nom2.1
#> 1 15.1   10.1 20.3   90.3
#> 2 45.5   35.5 40.1   80.1
#> 3 32.1   42.1 50.2   50.2

Note that even if the cbind keep duplicated names, when ordering they will be modified. The same will happen if you use any form of selection or indexing as it is not possible to properly index duplicate names. For example:

names(subset(df3, select = c("Nom1", "Nom1")))
#> [1] "Nom1"   "Nom1.1"

names(df3[, c(1,3)])
#> [1] "Nom1"   "Nom1.1"

If for any reason you really need the names to be kept duplicated, you will need to use some alternative to date frames.. data.table accepts duplicate names, but even in this case to reorder you will have to manually set the number of columns:

library(data.table)

setDT(df3)

df3[, c(1,3,2,4)]
#>    Nom1 Nom1 Nom2 Nom2
#> 1: 15.1 10.1 20.3 90.3
#> 2: 45.5 35.5 40.1 80.1
#> 3: 32.1 42.1 50.2 50.2

But This is highly not recommended. Keeping duplicate names is simply bad practice.

-2

  • Good afternoon! So, not sure with the rbind pq it does is join the columns vertically. I needed column 1 of df1 to side with column 1 of df2 and so on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.