📌  相关文章
📜  如何在 R 中基于多列合并 DataFrame?

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

如何在 R 中基于多列合并 DataFrame?

在本文中,我们将讨论如何在 R 编程语言中基于多列合并数据帧。

我们可以使用 merge()函数合并基于多列的两个数据框

语法

让我们创建两个数据框并显示

示例:R 程序创建两个具有 4 列学生数据的数据框

R
# create dataframe1
data1 = data.frame(name=c("manoj", "manoja", "saroja", "ramya"),
                   subjects=c("java", "c/cpp", "python", "R"),
                   city=c("ponnur", "tenali", "hyd", "guntur"),
                   marks=c(89, 90, 78, 89))
  
# create dataframe2
data2 = data.frame(name=c("manoj", "sravya", "saroja", "pavani"),
                   subjects=c("java", "c/html", "python", "php/css"),
                   city=c("ponnur", "hyd", "hyd", "guntur"),
                   marks=c(89, 78, 78, 81))
  
# display
print(data1)
print(data2)


R
# create dataframe1
data1 = data.frame(name=c("manoj", "manoja", "saroja", "ramya"),
                   subjects=c("java", "c/cpp", "python", "R"),
                   city=c("ponnur", "tenali", "hyd", "guntur"),
                   marks=c(89, 90, 78, 89))
  
# create dataframe2
data2 = data.frame(student=c("manoj", "sravya", "saroja", "pavani"),
                   subjects=c("java", "c/html", "python", "php/css"),
                   city=c("ponnur", "hyd", "hyd", "guntur"),
                   exams=c(89, 78, 78, 81))
  
# merge dataframes based on name and subjects from data1
# with student and subjects from data2
merge(data1, data2, by.x=c('name', 'subjects'),
      by.y=c('student', 'subjects'))


R
# create dataframe1
data1 = data.frame(name=c("manoj", "manoja", "saroja", "ramya"),
                   subjects=c("java", "c/cpp", "python", "R"),
                   city=c("ponnur", "tenali", "hyd", "guntur"),
                   marks=c(89, 90, 78, 89))
  
# create dataframe2
data2 = data.frame(student=c("manoj", "sravya", "saroja", "pavani"),
                   subjects=c("java", "c/html", "python", "php/css"),
                   city=c("ponnur", "hyd", "hyd", "guntur"),
                   exams=c(89, 78, 78, 81))
  
# merge dataframes based on name,city and subjects from data1
# with student,city and subjects from data2
merge(data1, data2, by.x=c('name', 'subjects', 'city'),
      by.y=c('student', 'subjects', 'city'))


输出

示例 1:合并 2 列上的数据框

R

# create dataframe1
data1 = data.frame(name=c("manoj", "manoja", "saroja", "ramya"),
                   subjects=c("java", "c/cpp", "python", "R"),
                   city=c("ponnur", "tenali", "hyd", "guntur"),
                   marks=c(89, 90, 78, 89))
  
# create dataframe2
data2 = data.frame(student=c("manoj", "sravya", "saroja", "pavani"),
                   subjects=c("java", "c/html", "python", "php/css"),
                   city=c("ponnur", "hyd", "hyd", "guntur"),
                   exams=c(89, 78, 78, 81))
  
# merge dataframes based on name and subjects from data1
# with student and subjects from data2
merge(data1, data2, by.x=c('name', 'subjects'),
      by.y=c('student', 'subjects'))

输出

示例 2:基于三列合并数据框。

R

# create dataframe1
data1 = data.frame(name=c("manoj", "manoja", "saroja", "ramya"),
                   subjects=c("java", "c/cpp", "python", "R"),
                   city=c("ponnur", "tenali", "hyd", "guntur"),
                   marks=c(89, 90, 78, 89))
  
# create dataframe2
data2 = data.frame(student=c("manoj", "sravya", "saroja", "pavani"),
                   subjects=c("java", "c/html", "python", "php/css"),
                   city=c("ponnur", "hyd", "hyd", "guntur"),
                   exams=c(89, 78, 78, 81))
  
# merge dataframes based on name,city and subjects from data1
# with student,city and subjects from data2
merge(data1, data2, by.x=c('name', 'subjects', 'city'),
      by.y=c('student', 'subjects', 'city'))

输出