首页 > 代码库 > R语言之merge详解
R语言之merge详解
merge函数的声明:
merge(x, y, by = intersect(names(x), names(y)), by.x = by, by.y = by, all = FALSE, all.x = all, all.y = all, sort = TRUE, suffixes = c(".x",".y"), incomparables = NULL, ...)
merge函数参数的说明:
x,y:用于合并的两个数据框
by,by.x,by.y:指定依据哪些行合并数据框,默认值为相同列名的列.
all,all.x,all.y:指定x和y的行是否应该全在输出文件.
sort:by指定的列是否要排序.
suffixes:指定除by外相同列名的后缀.
incomparables:指定by中哪些单元不进行合并.
例子:
w1: NAME SCHOOL CLASS ENGLISH A S1 10 85 B S2 5 50 A S1 4 90 A S1 11 90 C S1 1 12 w2: NAME SCHOOL CLASS MATHS ENGLISH A S3 5 80 88 B S2 5 89 81 C S1 1 55 32
按照NAME, SCHOOL, CLASS合并w1和w2:
merge(w1, w2, all = T) NAME SCHOOL CLASS ENGLISH MATHS 1 A S1 4 90 NA 2 A S1 10 85 NA 3 A S1 11 90 NA 4 A S3 5 88 80 5 B S2 5 50 NA 6 B S2 5 81 89 7 C S1 1 12 NA 8 C S1 1 32 55 merge(w1, w2, by = c("NAME", "SCHOOL", "CLASS"), all = T) NAME SCHOOL CLASS ENGLISH.x MATHS ENGLISH.y 1 A S1 4 90 NA NA 2 A S1 10 85 NA NA 3 A S1 11 90 NA NA 4 A S3 5 NA 80 88 5 B S2 5 50 89 81 6 C S1 1 12 55 32 merge(w1, w2, all = T, incomparables = "A") Error in merge.data.frame(w1, w2, all = T, incomparables = "A") : ‘incomparables‘ is supported only for merging on a single column merge(w1, w2, all = T, by = "NAME", incomparables = "A") NAME SCHOOL.x CLASS.x ENGLISH.x SCHOOL.y CLASS.y MATHS ENGLISH.y 1 A S1 10 85 <NA> NA NA NA 2 A S1 4 90 <NA> NA NA NA 3 A S1 11 90 <NA> NA NA NA 4 A <NA> NA NA S3 5 80 88 5 B S2 5 50 S2 5 89 81 6 C S1 1 12 S1 1 55 32
R语言之merge详解
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。