【3.4.1】merge--数据框合并

merge函数是用来合并数据框行或列,并可以按照指定排序对数据框进行排序。

一 、列的合并

> df1 = data.frame(CustomerId=c(1:6),Product=c(rep("Toaster",3),rep("Radio",3)))
> df2 = data.frame(CustomerId=c(2,4,6),State=c(rep("Alabama",2),rep("Ohio",1)))
> df1
 CustomerId Product
1 1 Toaster
2 2 Toaster
3 3 Toaster
4 4 Radio
5 5 Radio
6 6 Radio
> df2
 CustomerId State
1 2 Alabama
2 4 Alabama
3 6 Ohio

>merge(df1, df2, all=TRUE)   #df1,df2所有行都保留
 
 CustomerId Product State
1 1 Toaster 
2 2 Toaster Alabama
3 3 Toaster 
4 4 Radio Alabama
5 5 Radio 
6 6 Radio Ohio
>merge(df1, df2, all.x=TRUE)   #保留x的行
 
 CustomerId Product State
1 1 Toaster 
2 2 Toaster Alabama
3 3 Toaster 
4 4 Radio Alabama
5 5 Radio 
6 6 Radio Ohio
>merge(df1, df2, all.y=TRUE)    #保留y的行
 
 CustomerId Product State
1 2 Toaster Alabama
2 4 Radio Alabama
3 6 Radio Ohio
#####################################
d <- merge(d,cons.tax, by = "name", all = T)

R按照指定的顺序来排序Merge,只要把原始文件,匹配到顺序文件,

> d<-matrix(1:9,nrow=3,ncol=3)
> colnames(d)<-c("a","b","c")
> d
 a b c
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> View(d)
>d1<-matrix(c(3,2,1),nrow=3,ncol=1)
>
 colnames(d1)<-c("a")
> d1
 a
[1,] 3
[2,] 2
[3,] 1
> d2<-merge(d1,d,by="a",sort=F)
> d2
 a b c
1 3 6 9
2 2 5 8
3 1 4 7

二、行的合并

求共同的行

 rnames<-c("A","B","C","D","E");
 cnames<-c("a1","a2");
 a<-matrix(1:10,nrow=5,dimnames=list(rnames,cnames));
 rnames2<-c("D","A","N");
 cnames<-c("a1","a2");
 a2<-matrix(c(4,1,100,9,6,99),nrow=3, dimnames=list(rnames2,cnames));
 > a
 a1 a2
 A 1 6
 B 2 7
 C 3 8
 D 4 9
 E 5 10
 > a2
 a1 a2
 D 4 9
 A 1 6
 N 100 99

 >merge(a,a2,by=colnames(a))
 a
 1 a2
 1 1 6
 2 4 9
 >merge(a,a2,by=colnames(a2))
 a
 1 a2
 1 1 6
 2 4 9
 >merge(a,a2,by=colnames(a2),all=T)
 a
 1 a2
 1 1 6
 2 2 7
 3 3 8
 4 4 9
 5 5 10
 6 100 99
 > merge(a,a2,by=colnames(a2),all.X=T)
 a1 a2
 1 1 6
 2 4 9
 > merge(a,a2,by=colnames(a2),all.y=T)
 a1 a2
 1 1 6
 2 4 9
 3 100 99
 > merge(a,a2,by=colnames(a),all.y=T)
 a1 a2
 1 1 6
 2 4 9
 3 100 99
 > merge(a,a2,by=colnames(a),all.x=T)
 a1 a2
 1 1 6
 2 2 7
 3 3 8
 4 4 9
 5 5 10

参考资料:

沉闷科学的掘墓人 http://bbs.sciencenet.cn/blog-508298-652589.html

stacworkflow http://stackoverflow.com/questions/1299871/how-to-join-data-frames-in-r-inner-outer-left-right

这里是一个广告位,,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn