📜  如何合并 R 中的数据帧?

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

如何合并 R 中的数据帧?

在本文中,我们将讨论如何使用 R 编程语言在给定的数据帧中执行内部、外部、左或右连接。

使用的功能

merge()函数用于合并或连接两个表。通过为特定参数提供适当的值,我们可以创建所需的连接。

内部联接

内部连接也称为自然连接,将两个数据帧合并为一个,其中包含两者的公共元素。对于这个 merge()函数,简单地给出了两个数据帧的值,并在一个公共列的基础上生成一个数据帧。

语法



例子

R
# create data frame 1 with id ,
# name and address 
df1=data.frame(id=c(7058,7059,7072,7075), 
               name=c("bobby","pinkey","harsha","deepika"),
               address=c("kakumanu","hyd","tenali","chebrolu"))
  
# create data frame 2 with id , 
# marks  
df2=data.frame(id=c(7058,7059,7072,7075,7062,7063), 
               marks=c(90,78,98,67,89,90))
  
# display dataframe1
print(df1)
  
# display dataframe2
print(df2)
  
print("Inner join")
  
# inner join 
print(merge(x = df1, y = df2))


R
# create data frame 1 with id , name and address 
df1=data.frame(id=c(7058,7059,7072,7075), 
               name=c("bobby","pinkey","harsha","deepika"),
               address=c("kakumanu","hyd","tenali","chebrolu"))
  
# create data frame 2 with id , marks  
df2=data.frame(id=c(7058,7059,7072,7075,7062,7063), 
               marks=c(90,78,98,67,89,90))
  
# display dataframe1
print(df1)
  
# display dataframe2
print(df2)
  
print("Inner join")
  
# outer join 
print(merge(x = df1, y = df2,all=TRUE))


R
# create data frame 1 with id , name and address 
df1=data.frame(id=c(7058,7059,7072,7075), 
               name=c("bobby","pinkey","harsha","deepika"),
               address=c("kakumanu","hyd","tenali","chebrolu"))
  
# create data frame 2 with id , marks  
df2=data.frame(id=c(7058,7059,7072,7075,7062,7063), 
               marks=c(90,78,98,67,89,90))
  
# display dataframe1
print(df1)
  
# display dataframe2
print(df2)
  
print("Left join")
# Left join 
print(merge(x = df1, y = df2,all.x=TRUE))


R
# create data frame 1 with id , name and address 
df1=data.frame(id=c(7058,7059,7072,7075), 
               name=c("bobby","pinkey","harsha","deepika"),
               address=c("kakumanu","hyd","tenali","chebrolu"))
  
# create data frame 2 with id , marks  
df2=data.frame(id=c(7058,7059,7072,7075,7062,7063), 
               marks=c(90,78,98,67,89,90))
  
# display dataframe1
print(df1)
  
# display dataframe2
print(df2)
  
print("Right  join")
# Right  join 
print(merge(x = df1, y = df2,all.y=TRUE))


输出:

外连接

外连接将两个数据框的所有列合并为所有元素的一列。为此,必须将考虑中的数据帧以及所有参数赋值为 TRUE 传递给 merge()函数。

语法



例子:

电阻

# create data frame 1 with id , name and address 
df1=data.frame(id=c(7058,7059,7072,7075), 
               name=c("bobby","pinkey","harsha","deepika"),
               address=c("kakumanu","hyd","tenali","chebrolu"))
  
# create data frame 2 with id , marks  
df2=data.frame(id=c(7058,7059,7072,7075,7062,7063), 
               marks=c(90,78,98,67,89,90))
  
# display dataframe1
print(df1)
  
# display dataframe2
print(df2)
  
print("Inner join")
  
# outer join 
print(merge(x = df1, y = df2,all=TRUE))

输出:

注意:它返回不匹配列的 NA

左加入

它给出将第一个数据框中的所有行与第二个数据框中的相应值匹配的数据。为此,连同考虑中的数据帧,在给出左表的引用后,所有参数都必须传递为 TRUE。

语法

例子:

电阻

# create data frame 1 with id , name and address 
df1=data.frame(id=c(7058,7059,7072,7075), 
               name=c("bobby","pinkey","harsha","deepika"),
               address=c("kakumanu","hyd","tenali","chebrolu"))
  
# create data frame 2 with id , marks  
df2=data.frame(id=c(7058,7059,7072,7075,7062,7063), 
               marks=c(90,78,98,67,89,90))
  
# display dataframe1
print(df1)
  
# display dataframe2
print(df2)
  
print("Left join")
# Left join 
print(merge(x = df1, y = df2,all.x=TRUE))

输出:

右加入

它给出了将第二个数据框中的所有行与第一个数据框中的相应值匹配的数据。对于此 merge()函数,应提供数据帧以及分配为 TRUE 的所有参数。所有参数都应该引用正确的数据帧。

语法

例子:

电阻

# create data frame 1 with id , name and address 
df1=data.frame(id=c(7058,7059,7072,7075), 
               name=c("bobby","pinkey","harsha","deepika"),
               address=c("kakumanu","hyd","tenali","chebrolu"))
  
# create data frame 2 with id , marks  
df2=data.frame(id=c(7058,7059,7072,7075,7062,7063), 
               marks=c(90,78,98,67,89,90))
  
# display dataframe1
print(df1)
  
# display dataframe2
print(df2)
  
print("Right  join")
# Right  join 
print(merge(x = df1, y = df2,all.y=TRUE))

输出: