1. 程式人生 > >R語言中的資料框合併

R語言中的資料框合併

#兩個資料框有相同和不同列----合併
ID<-c(1,2,3,4)
name<-c("A","B","C","D")
score<-c(60,70,80,90)
student1<-data.frame(ID,name)
student2<-data.frame(ID,score)
total_student1<-merge(student1,student2,by="ID")
total_student1

#橫向追加----兩個資料框不同列

ID<-c(1,2,3,4)
name<-c("A","B","C","D")
score<-c(60,70,80,90)
sex<-c("M","F","M","M")
student1<-data.frame(ID,name)
student2<-data.frame(score,sex)
total_student2<-cbind(student1,student2)
total_student2

#縱向追加---
兩個資料框同列
ID<-c(1,2,3,4)
name<-c("A","B","C","D")
student1<-data.frame(ID,name)
ID<-c(5,6,7,8)
name<-c("E","F","G","H")
student2<-data.frame(ID,name)
total_student3<-rbind(student1,student2)

total_student3

#合併兩列資料為一列  檢視型別class(t$V1)
x=paste(t$V1,t$V2, sep = "")