R语言中merge函数详解

来自:网络
时间:2021-11-06
阅读:

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

R语言中merge函数详解

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

R语言中merge函数详解

2、查看两个数据框

w
q

R语言中merge函数详解

3、指定匹配列进行合并(按照行合并)

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

R语言中merge函数详解

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

R语言中merge函数详解

4、指定匹配列合并,没有的内容填充为NA

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

R语言中merge函数详解

5、依照左侧数据进行匹配

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

R语言中merge函数详解

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

R语言中merge函数详解

6、依照右侧数据进行匹配

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

R语言中merge函数详解

返回顶部
顶部