📌  相关文章
📜  通过 R 编程中的公共列合并两个数据框 - merge()函数

📅  最后修改于: 2022-05-13 01:55:48.685000             🧑  作者: Mango

通过 R 编程中的公共列合并两个数据框 - merge()函数

R 语言中的merge()函数用于按公共列合并两个数据帧。

示例 1:

# R program to merge two data frames
  
# Creating data frames
df1 <- data.frame(row1 = c("a", "b", "c"), row2 = c("d", "e", "f"))
df2 <- data.frame(col1 = c("a", "b", "c"), col2 = c("Hello", "Geeks", "gfg"))
  
# Calling merge() function
df <- merge(df1, df2, by.x ="row1", by.y ="col1")
print(df)

输出:

row1 row2  col2
1    a    d Hello
2    b    e Geeks
3    c    f   gfg

示例 2:

# R program to merge two data frames
  
# Creating data frames
df1 <- data.frame(row1 = c("d", "e", "f"), row2 = c(1, 2, 3))
df2 <- data.frame(col1 = c(1, 2, 3), col2 = c("Hello", "Geeks", "gfg"))
  
# Calling merge() function
df <- merge(df1, df2, by.x ="row2", by.y ="col1")
print(df)

输出:

row2 row1  col2
1    1    d Hello
2    2    e Geeks
3    3    f   gfg