R語言中merge函數


 

來源:https://blog.csdn.net/u010652755/article/details/72982227/

 

1、創建測試數據:

name <- c('A','B','A','A','C','D')
school <- c('s1','s2','s1','s1','s1','s3')
class <- c(10, 5, 4, 11, 1, 8)
English <- c(85, 50, 90 ,90, 12, 96)
w <- data.frame(name, school, class, English)
w

 

 

name  <-  c('A','B','C','F')
school <- c('s3','s2','s1','s2')
class <-  c(5, 5, 1,3)
maths <- c(80,89,55,90)
English <- c(88, 89, 32, 89)
q <- data.frame(name, school, class, maths, English)
q

 

 

2、查看兩個數據框

w
q

 

 

3、指定匹配列進行合並(按照行合並)

merge(w,q,by.x = 'name', by.y = 'name')

 

 

w
q
merge(w,q,by.x = 'school', by.y = 'school')

 

 

4、指定匹配列合並,沒有的內容填充為NA

w
q
merge(w, q, all=TRUE, sort=TRUE) 

 

 

5、依照左側數據進行匹配

w
q
merge(w ,q ,all.x=TRUE,sort=TRUE)

 

 

w
q
merge(w, q, by = 'name',all.x = TRUE, sort = TRUE)

 

 

6、依照右側數據進行匹配

w
q
merge(w ,q ,by = 'name', all.y=TRUE,sort=TRUE) 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM