R语言数据框合并(merge)的几种方式小结
mergedataframes(inner,outer,left,right)
数据
>df1=data.frame(CustomerId=c(1:6),Product=c(rep("Toaster",3),rep("Radio",3))) >df1 >CustomerIdProduct 11Toaster 22Toaster 33Toaster 44Radio 55Radio 66Radio >df2=data.frame(CustomerId=c(2,4,6),State=c(rep("Alabama",2),rep("Ohio",1))) >df >CustomerIdState 12Alabama 24Alabama 36Ohio
inner
>merge(x=df1,y=df2,by="CustomerId",all=TRUE) >CustomerIdProductState 11Toaster22ToasterAlabama 33Toaster 44RadioAlabama 55Radio 66RadioOhio
left
>merge(x=df1,y=df2,by="CustomerId",all.x=TRUE) >CustomerIdProductState 11Toaster22ToasterAlabama 33Toaster 44RadioAlabama 55Radio 66RadioOhio
right
>merge(x=df1,y=df2,by="CustomerId",all.y=TRUE) >CustomerIdProductState 12ToasterAlabama 24RadioAlabama 36RadioOhio
outer
>merge(x=df1,y=df2,by=NULL) >CustomerId.xProductCustomerId.yState 11Toaster2Alabama 22Toaster2Alabama 33Toaster2Alabama 44Radio2Alabama 55Radio2Alabama 66Radio2Alabama 71Toaster4Alabama 82Toaster4Alabama 93Toaster4Alabama 104Radio4Alabama 115Radio4Alabama 126Radio4Alabama 131Toaster6Ohio 142Toaster6Ohio 153Toaster6Ohio 164Radio6Ohio 175Radio6Ohio 186Radio6Ohio
到此这篇关于R语言数据框合并(merge)的几种方式小结的文章就介绍到这了,更多相关R语言数据框合并内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。