0

I'm looking for a dplyr solution to merge two df (or tibble) in dplyr, when they have the same langth, but different column names.

The solutions I found so far relate to a common column name, joined through the by = "" field.

The structure:

df1

A B
1 4
2 2

df2

C D
4 1
3 4

Desired output:

df

A B C D
1 4 4 1
2 2 3 4
4

1 回答 1

1

You can try this:

library(data.table)
setDT(df1,keep.rownames=T); setDT(df2,keep.rownames=T)
df3 <- df1[df2,on="rn"]
df3 <- subset(df3, select = -c(rn))

Approach taken from here.

EDIT

You could reach a similar thing with dplyr

df1$rn <- seq.int(nrow(df1))
df2$rn <- seq.int(nrow(df2))
df3 <- full_join(df1, df2, by = rn)
于 2017-11-28T14:34:58.520 回答