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.
merge(df1, df2, by = "row.names")
?– neves
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
– c_mariano