📌  相关文章
📜  如何在R中的两个数据帧之间找到公共行?

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

如何在R中的两个数据帧之间找到公共行?

在本文中,我们将在 R 编程语言中找到两个数据帧之间的公共行。

为此,我们首先创建两个数据框。正在使用的数据帧:

方法一:使用内连接

我们可以通过对两个数据帧执行内部连接来获取公共行。它在 dplyr() 包中可用

句法:



在哪里,

  • data1 是第一个数据帧
  • data2 是第二个数据帧

示例: R 程序查找两个数据帧中的公共行

R
# load the dplyr library
library("dplyr")
  
# create a dataframe with 3 columns for college1
data1 = data.frame(id=c(1, 2, 3),
                   name=c("sravan", "ojsawi", "rohith"),
                   dept=c("CS", "EC", "IT"))
  
print(data1)
  
# create a dataframe with 3 columns for college2
data2 = data.frame(id=c(1, 2, 3),
                   name=c("ramya", "ojsawi", "rohith"),
                   dept=c("Nech", "EC", "IT"))
  
print(data2)
  
# get the common rows by using inner join
inner_join(data1, data2)


R
# load the dplyr library
library("dplyr")
  
# create a dataframe with 3 columns for college1
data1 = data.frame(id=c(1, 2, 3, 4, 5),
                   name=c("sravan", "ojsawi", "rohith", "bobby", "gnanesh"),
                   dept=c("CS", "EC", "IT", "CS", "CS"))
  
print(data1)
  
# create a dataframe with 3 columns for college2
data2 = data.frame(id=c(1, 2, 3, 4),
                   name=c("ramya", "ojsawi", "rohith", "bobby"),
                   dept=c("Nech", "EC", "IT", "CS"))
  
print(data2)
  
# get the common rows by using inner join
inner_join(data1, data2)


R
# load the dplyr library
library("dplyr")
  
# load generics library
library("generics")
  
# create a dataframe with 3 columns for college1
data1 = data.frame(id=c(1, 2, 3, 4, 5),
                   name=c("sravan", "ojsawi", "rohith", "bobby", "gnanesh"),
                   dept=c("CS", "EC", "IT", "CS", "CS"))
  
print(data1)
  
# create a dataframe with 3 columns for college2
data2 = data.frame(id=c(1, 2, 3, 4),
                   name=c("ramya", "ojsawi", "rohith", "bobby"),
                   dept=c("Nech", "EC", "IT", "CS"))
  
print(data2)
  
# get the common rows by using intersect
print(generics: : intersect(data1, data2))


输出:

示例:从数据框中获取公共行的 R 程序

电阻



# load the dplyr library
library("dplyr")
  
# create a dataframe with 3 columns for college1
data1 = data.frame(id=c(1, 2, 3, 4, 5),
                   name=c("sravan", "ojsawi", "rohith", "bobby", "gnanesh"),
                   dept=c("CS", "EC", "IT", "CS", "CS"))
  
print(data1)
  
# create a dataframe with 3 columns for college2
data2 = data.frame(id=c(1, 2, 3, 4),
                   name=c("ramya", "ojsawi", "rohith", "bobby"),
                   dept=c("Nech", "EC", "IT", "CS"))
  
print(data2)
  
# get the common rows by using inner join
inner_join(data1, data2)

输出:

方法二:使用 intersect()函数

此函数用于从数据框中获取公共行。这个函数在 generics 包中可用,所以我们需要先导入这个包。

句法:

在哪里

  • dataframe1 是第一个数据帧
  • dataframe2 是第二个数据帧

示例: R 程序使用 intersect()函数获取公共行

电阻

# load the dplyr library
library("dplyr")
  
# load generics library
library("generics")
  
# create a dataframe with 3 columns for college1
data1 = data.frame(id=c(1, 2, 3, 4, 5),
                   name=c("sravan", "ojsawi", "rohith", "bobby", "gnanesh"),
                   dept=c("CS", "EC", "IT", "CS", "CS"))
  
print(data1)
  
# create a dataframe with 3 columns for college2
data2 = data.frame(id=c(1, 2, 3, 4),
                   name=c("ramya", "ojsawi", "rohith", "bobby"),
                   dept=c("Nech", "EC", "IT", "CS"))
  
print(data2)
  
# get the common rows by using intersect
print(generics: : intersect(data1, data2))

输出: