📌  相关文章
📜  如何在 R 中合并多个数据帧?

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

如何在 R 中合并多个数据帧?

在本文中,我们将讨论如何在 R 编程语言中合并多个数据帧。数据框可以按行和列合并,我们可以使用cbind()函数合并列,使用rbind()函数合并行

按列合并

cbind()用于按列组合数据帧。

示例 1:

R
# vector with student details
names1=c("sravan","bobby","ojaswi")
  
# vector with marks
marks1=c(90,89,78)
  
# pass these vectors to the 
# dataframe 1
data1=data.frame(names1=names1,marks1=marks1)
print(data1)
  
# vector with student details
names2=c("gnanesh","rohith","divya")
  
# vector with marks
marks2=c(68,99,79)
  
# pass these vectors to the dataframe 2
data2=data.frame(names2=names2,marks2=marks2)
print(data2)
  
print("-------------------------------\
-------------------------------")
  
# merging these two dataframes using cbind
print(cbind(data1,data2))


R
# vector with student details
names1=c("sravan","bobby","ojaswi")
  
# vector with marks
marks1=c(90,89,78)
  
# pass these vectors to the
# dataframe 1
data1=data.frame(names1=names1,marks1=marks1)
print(data1)
  
# vector with student details
names2=c("gnanesh","rohith","divya")
  
# vector with marks
marks2=c(68,99,79)
  
# pass these vectors to the dataframe 2
data2=data.frame(names2=names2,marks2=marks2)
print(data2)
  
print("---------------------------------
\-----------------------------")
  
# merging these two data frames marks
# column using cbind
print(cbind(data1$marks1,data2$marks2))


R
# vector with student details
names1=c("sravan","bobby","ojaswi")
  
# vector with marks
marks1=c(90,89,78)
  
# pass these vectors to the 
# dataframe 1
data1=data.frame(names1=names1,marks1=marks1)
print(data1)
  
# vector with student details
names2=c("gnanesh","rohith","divya")
  
# vector with marks
marks2=c(68,99,79)
  
# pass these vectors to the dataframe 2
data2=data.frame(names2=names2,marks2=marks2)
print(data2)
  
# vector with student details
names3=c("bhavya","harsha","navya")
  
# vector with marks
marks3=c(68,99,79)
  
# pass these vectors to the dataframe 3
data3=data.frame(names3=names3,marks3=marks3)
print(data3)
  
print("-----------------------------------\
---------------------------")
  
# merging these three data frames
print(cbind(data1,data2,data3))


R
# create vectors
x=c(1,2,3,4,3,4,5)
y=c(4,5,6,7,2,3,4)
  
# pass these vectors to the 
# input of dataframe1(a)
a=data.frame(x,y)
  
# create vectors
x=c(10,20,30,40,50,60,70)
y=c(40,50,60,40,50,60,70)
  
# pass these vectors to the 
# input of dataframe1(a)
b=data.frame(x,y)
  
# apply rbind function to 
# merge rows
print(rbind(a,b))


R
# create vectors
x=c(1,2,3,4,3,4,5)
y=c(4,5,6,7,2,3,4)
  
# pass these vectors to the 
# input of dataframe1(a)
a=data.frame(x,y)
  
# display dataframe
print(a)
print("---------------------")
  
# create vectors
x=c("sravan","bobby")
y=c("Eswar","sai")
  
# pass these vectors to the 
# input of dataframe1(a)
b=data.frame(x,y)
  
# display dataframe
print(b)
  
# apply rbind function to merge rows
print(rbind(a,b))


输出:

示例 2:

我们还可以通过使用 $运算符符合并每个数据帧中的特定列,我们可以访问数据帧列

语法



电阻

# vector with student details
names1=c("sravan","bobby","ojaswi")
  
# vector with marks
marks1=c(90,89,78)
  
# pass these vectors to the
# dataframe 1
data1=data.frame(names1=names1,marks1=marks1)
print(data1)
  
# vector with student details
names2=c("gnanesh","rohith","divya")
  
# vector with marks
marks2=c(68,99,79)
  
# pass these vectors to the dataframe 2
data2=data.frame(names2=names2,marks2=marks2)
print(data2)
  
print("---------------------------------
\-----------------------------")
  
# merging these two data frames marks
# column using cbind
print(cbind(data1$marks1,data2$marks2))

输出:

示例 3:

电阻

# vector with student details
names1=c("sravan","bobby","ojaswi")
  
# vector with marks
marks1=c(90,89,78)
  
# pass these vectors to the 
# dataframe 1
data1=data.frame(names1=names1,marks1=marks1)
print(data1)
  
# vector with student details
names2=c("gnanesh","rohith","divya")
  
# vector with marks
marks2=c(68,99,79)
  
# pass these vectors to the dataframe 2
data2=data.frame(names2=names2,marks2=marks2)
print(data2)
  
# vector with student details
names3=c("bhavya","harsha","navya")
  
# vector with marks
marks3=c(68,99,79)
  
# pass these vectors to the dataframe 3
data3=data.frame(names3=names3,marks3=marks3)
print(data3)
  
print("-----------------------------------\
---------------------------")
  
# merging these three data frames
print(cbind(data1,data2,data3))

输出:

按行合并

我们可以使用 rbind()函数合并数据帧之间的行。

句法:



示例 1:

电阻

# create vectors
x=c(1,2,3,4,3,4,5)
y=c(4,5,6,7,2,3,4)
  
# pass these vectors to the 
# input of dataframe1(a)
a=data.frame(x,y)
  
# create vectors
x=c(10,20,30,40,50,60,70)
y=c(40,50,60,40,50,60,70)
  
# pass these vectors to the 
# input of dataframe1(a)
b=data.frame(x,y)
  
# apply rbind function to 
# merge rows
print(rbind(a,b))

输出:

示例 2:

电阻

# create vectors
x=c(1,2,3,4,3,4,5)
y=c(4,5,6,7,2,3,4)
  
# pass these vectors to the 
# input of dataframe1(a)
a=data.frame(x,y)
  
# display dataframe
print(a)
print("---------------------")
  
# create vectors
x=c("sravan","bobby")
y=c("Eswar","sai")
  
# pass these vectors to the 
# input of dataframe1(a)
b=data.frame(x,y)
  
# display dataframe
print(b)
  
# apply rbind function to merge rows
print(rbind(a,b))

输出: