Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can cause a “non-unique matches detected” error in an r merge?

Tags:

merge

r

I am trying to merge a spatial polygons data frame and a data frame, but I keep getting a “non-unique matches detected” error. I have run duplicated() on the columns used for by.x and by.y and no duplicates are detected. What else could cause this error?

> head(AnteNatal.df)
              Country Year(s) AnteNatalValue
1         Afghanistan    2011           14.6
2             Albania    2009           66.8
3              Angola    2009           47.1
4 Antigua and Barbuda    2011          100.0
5           Argentina    2006           24.7
6             Armenia    2010           92.8

> Africa.sh$NAME[1:5]
[1] Angola       Burundi      Benin        Burkina Faso Botswana    
243 Levels: Afghanistan Aland Albania Algeria American Samoa Andorra Angola        Anguilla Antarctica ... Zimbabwe

The calls

duplicated(Africa.sh$NAME)
duplicated(AnteNatal.df$Country)

both return lists containing all FALSE. However, when I try to merge, I get

> merge(Africa.sh,AnteNatal.df, by.x = "NAME", by.y ="Country")
Error in .local(x, y, ...) : non-unique matches detected
like image 523
crr Avatar asked Jul 07 '16 23:07

crr


1 Answers

Use the sp::merge function with the duplicateGeoms argument set to TRUE (if TRUE geometries in x are duplicated if there are multiple matches between records in x and y)

    require(sp)
    ?sp::merge
    merge(spatial_data, data_frame, by = 'match_column', duplicateGeoms = TRUE)

(I know this question is older, but I came across it while in search for an answer to it myself)

like image 190
avianattackarmada Avatar answered Sep 21 '22 20:09

avianattackarmada