并排粘贴 2 个数据框,无需任何键

Ian*_*.T 2 merge r melt reshape2 dplyr

我有两个数据框

A   B    E   H  
x1  x2  x3  x6  
x1  x2  x4  x7  
x1  x2  x5  x8  

Run Code Online (Sandbox Code Playgroud)

A   B     
y1  y2    
y1  y2     
Run Code Online (Sandbox Code Playgroud)

这就是我想用 dplyr 或 reshape2 实现的

A   B    E   H  A   B   
x1  x2  x3  x6  y1  y2  
x1  x2  x4  x7  y1  y2   
x1  x2  x5  x8      
Run Code Online (Sandbox Code Playgroud)

谢谢

akr*_*run 5

如果行数相同,则使用

cbind(df1, df2)
#   A  B  E  H  A  B
#1 x1 x2 x3 x6 y1 y2
#2 x1 x2 x4 x7 y1 y2
#3 x1 x2 x5 x8 y1 y2
Run Code Online (Sandbox Code Playgroud)

或者在 dplyr

library(dplyr)
library(stringr)
df2 %>% 
       rename_all(~ str_c(., ".1")) %>%
       bind_cols(df1, .)
Run Code Online (Sandbox Code Playgroud)

dplyr( 0.8.5) 的某些版本中,当有重复的列名时,它会正确重命名

bind_cols(df1, df2)
Run Code Online (Sandbox Code Playgroud)

注意:不建议使用相同的列名,data.frame以便我们可以更改列名make.unique


如果我们有两个行数不等的数据集

library(rowr)
cbind.fill(df1, df2new, fill = NA)
#   A  B  E  H    A    B
#1 x1 x2 x3 x6   y1   y2
#2 x1 x2 x4 x7   y1   y2
#3 x1 x2 x5 x8 <NA> <NA>
Run Code Online (Sandbox Code Playgroud)

或与 base R

mxn <- max(nrow(df1), nrow(df2new))
df2new[(nrow(df2new)+1):mxn,] <- NA
cbind(df1, df2new)
#   A  B  E  H    A    B
#1 x1 x2 x3 x6   y1   y2
#2 x1 x2 x4 x7   y1   y2
#3 x1 x2 x5 x8 <NA> <NA>
Run Code Online (Sandbox Code Playgroud)

数据

df1 <- structure(list(A = c("x1", "x1", "x1"), B = c("x2", "x2", "x2"
), E = c("x3", "x4", "x5"), H = c("x6", "x7", "x8")),
    class = "data.frame", row.names = c(NA, 
-3L))

df2 <- structure(list(A = c("y1", "y1", "y1"), B = c("y2", "y2", "y2"
)), class = "data.frame", row.names = c(NA, -3L))

df2new <- structure(list(A = c("y1", "y1"), B = c("y2", "y2")), class = "data.frame", row.names = c(NA, 
-2L))
Run Code Online (Sandbox Code Playgroud)

  • 即使您不采取任何操作,至少“dplyr::bind_cols(df1, df2)”也会自动将第二列“A”重命名为“A1” (2认同)