[R] Merging multiple data sets

Dennis Murphy djmuser at gmail.com
Thu Jun 23 19:13:31 CEST 2011


Hi:

Try this:

merge(merge(dat0, dat1, by = 'id', all.x = TRUE, all.y = TRUE), dat2,
by = 'id', all.x = TRUE, all.y = TRUE)

Dennis

On Thu, Jun 23, 2011 at 9:53 AM, cddesjar <cddesjardins at gmail.com> wrote:
> Hi,
> I am trying to merge data similar to the example data below
>
>> dat0
>
> id    var1    var2    var3
> 2       1         0         1
> 3       1         0         1
> 4       0         1         1
> 5       0         1         1
>
>> dat1
>
> id    var4    var5    var6
> 2       1         0         1
> 3       1         0         1
> 6       0         1         1
> 7       0         1         1
>
>> dat2
>
> id    var7    var8    var9
> 2       1         0         1
> 5       1         0         1
> 6       0         1         1
> 8       0         1         1
>
> Basically what I'd like to do is combine these variables on id and create
> one large data frame that looks like the following.
>
>> dat3
>
> id    var1    var2    var3   var4    var5   var6   var7   var8   var9
> 2       1         0         1      1         0        1      1        0
> 1
> 3       1         0         1      1         0        1      NA     NA
> NA
> 4       0         1         1      NA      NA      NA    NA     NA     NA
> 5       0         1         1      NA      NA      NA    NA     NA     NA
> 6      NA       NA       NA     0         1        1      0       1        1
> 7      NA       NA       NA     0         1        1     NA     NA     NA
> 8      NA       NA       NA     NA      NA     NA     0        1        1
>
> How can I best do this in R? I've looked into merge but it excludes ids that
> aren't in all 3 data sets.
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Merging-multiple-data-sets-tp3620431p3620431.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list