[R] Help with merge function

Rui Barradas ruipbarradas at sapo.pt
Fri Apr 26 20:25:00 CEST 2013


Hello,

The following seems to do the trick.



x1 <-
structure(list(State_prov = c("Nova Scotia", "Nova Scotia", "Nova Scotia"
), Shape_name = c("Annapolis", "Antigonish", "Gly"), bob2009 = c(0L,
0L, NA), bob2010 = c(0L, 0L, NA), bob2011 = c(1L, 0L, NA)), .Names = 
c("State_prov",
"Shape_name", "bob2009", "bob2010", "bob2011"), class = "data.frame", 
row.names = c(NA,
-3L))

x2 <-
structure(list(FID = 0:2, State_prov = c("Nova Scotia", "Nova Scotia",
"Nova Scotia"), Shape_name = c("Annapolis", "Antigonish", "Gly"
), bob2009 = c(0L, 0L, 0L), bob2010 = c(0L, 0L, 0L), coy2009 = c(10L,
1L, 1L)), .Names = c("FID", "State_prov", "Shape_name", "bob2009",
"bob2010", "coy2009"), class = "data.frame", row.names = c(NA,
-3L))

x3  <- merge(x1, x2, all.y = TRUE)



Note also that since by = intersect(names(x1), names(x2)), you really 
don't need it, it's the default behavior.

Hope this helps,

Rui Barradas

Em 26-04-2013 18:10, Catarina Ferreira escreveu:
> Dear all,
>
> I'm trying to merge 2 dataframes, but I'm not being entirely successful and
> I can't understand why.
>
> Dataframe x1
>
> State_prov     Shape_name   bob2009   bob 2010   bob2011
> Nova Scotia    Annapolis         0                  0              1
> Nova Scotia    Antigonish        0                  0              0
> Nova Scotia    Gly                   NA               NA             NA
>
> Dataframe x2 - has 20000 rows and 193 variables, contains one important
> field which is "FID" that is a link to a shapefile (this is not in x1) and
> shares common columns with x1, like this:
>
> FID     State_prov     Shape_name   bob2009   bob 2010  coy 2009
>   0        Nova Scotia    Annapolis         0
> 0              10
>   1        Nova Scotia    Antigonish        0
> 0              1
>   2        Nova Scotia    Gly                   0
> 0              1
>
> So when I do
>
> x3  <- merge(x1, x2, by=intersect(names(x1), names(x2)), all=TRUE)
>
> it should do the trick. The thing is that it works for the columns (it adds
> all the new columns not common to both dataframes), but it also adds the
> rows. This is what I get (x3):
>
> FID     State_prov     Shape_name   bob2009   bob 2010  coy 2009   bob2011
>   0        Nova Scotia    Annapolis         0
> 0              10            NA
>   NA      Nova Scotia    Annapolis         NA               NA          NA
>              1
>   1        Nova Scotia    Antigonish        0
> 0              1               NA
> NA      Nova Scotia    Antigonish        NA               NA          NA
>              0
>   2        Nova Scotia    Gly                   0
> 0              1               NA
> NA      Nova Scotia    Gly                   NA               NA
> NA             NA
>
> What I want to get is a true merge, like this:
>
> FID     State_prov     Shape_name   bob2009   bob 2010  coy 2009   bob2011
>   0        Nova Scotia    Annapolis         0
> 0              10            1
>   1        Nova Scotia    Antigonish        0
> 0              1               0
>   2        Nova Scotia    Gly                   0
> 0              1               NA
>
> Can anybody please help me to understand what I'm doing wrong.
> Any help will be much appreciated!!
>
>



More information about the R-help mailing list