|||
本文转自“stackExchange”网站,原文地址
http://stackoverflow.com/questions/1299871/how-to-join-data-frames-in-r-inner-outer-left-right
#########################
> 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)
CustomerId Product State
1 1 Toaster <NA>
2 2 Toaster Alabama
3 3 Toaster <NA>
4 4 Radio Alabama
5 5 Radio <NA>
6 6 Radio Ohio
> merge(df1, df2, all.x=TRUE)
CustomerId Product State
1 1 Toaster <NA>
2 2 Toaster Alabama
3 3 Toaster <NA>
4 4 Radio Alabama
5 5 Radio <NA>
6 6 Radio Ohio
> merge(df1, df2, all.y=TRUE)
CustomerId Product State
1 2 Toaster Alabama
2 4 Radio Alabama
3 6 Radio Ohio
#####################################
评述:
1、dataframe的填充默认按列填充
2、merge()函数较我们之前介绍的 cbind()和rbind()两个函数更加‘智能’
http://blog.sciencenet.cn/blog-508298-601709.html
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-27 11:46
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社