在 R 中水平组合两个数据框

-2 r

我想在 R 中水平组合两个数据框。

这是我的两个数据框:

数据框1:

veg     loc    quantity 

carrot  sak    three
pepper  lon    two
tomato  apw    five
Run Code Online (Sandbox Code Playgroud)

数据框2:

seller   quantity  veg

Ben      eleven    eggplant  
Nour     six       potato
Loni     four      zucchini
Ahmed    two       broccoli
Run Code Online (Sandbox Code Playgroud)

我希望结果是一个如下所示的数据框:

veg       quantity

carrot    three
pepper    two
tomato    five
eggplant  eleven
potato    six
zucchini  four
broccoli  two
Run Code Online (Sandbox Code Playgroud)

G. *_*eck 5

问题说“水平”,但从示例输出来看,您的意思似乎是“垂直”。

现在,假设输入在最后的注释中可重复显示,rbind他们就像这样。不使用任何包,也不覆盖任何对象。

sel <- c("veg", "quantity")
rbind( df1[sel], df2[sel] )
Run Code Online (Sandbox Code Playgroud)

如果您愿意,可以将第一行代码替换为以下代码,该代码会选出给出相同结果的公共列sel

sel <- intersect(names(df1), names(df2))
Run Code Online (Sandbox Code Playgroud)

笔记

Lines1 <- "veg     loc    quantity 
carrot  sak    three
pepper  lon    two
tomato  apw    five"

Lines2 <- "seller   quantity  veg
Ben      eleven    eggplant  
Nour     six       potato
Loni     four      zucchini
Ahmed    two       broccoli"

df1 <- read.table(text = Lines1, header = TRUE, strip.white = TRUE)
df2 <- read.table(text = Lines2, header = TRUE, strip.white = TRUE)
Run Code Online (Sandbox Code Playgroud)