Search code examples
rdataframecoordinates

How to eliminate rows from a dataframe based on coordinates in R


I'm usign two dataframes that contain coordinates (x,y) and a value for precipitation, since I'm trying to plot maps for the distribution od the rainfall in a certain area.

The contents of my datadrames are the following

head(cruproc,n=10)
        x     y     value
1  -97.75 15.75 0.1354839
2  -97.25 15.75 0.1419355
3  -96.75 15.75 0.0000000
4  -96.25 15.75 0.0000000
5  -95.75 15.75 0.1580645
6  -93.75 15.75 0.2129032
7  -93.25 15.75 0.1096774
8  -92.75 15.75 0.1419355
9  -92.25 15.75 0.4322581
10 -91.75 15.75 0.8483871
head(interp.df,n=10)
        x      y        pr
1  -97.75 -52.25 4.4805383
2  -97.25 -52.25 4.0390802
3  -97.75 -51.75 3.6501175
4  -97.25 -51.75 3.3274986
5  -97.75 -51.25 2.9044791
6  -97.25  15.75 3.0526965
7  -97.75 -49.75 2.7278418
8  -97.25 -49.75 2.8603867
9  -92.75  15.75 2.6617398
10 -93.25  15.75 2.6521587

As you can see many of the coordinates present in interp.df don't exist in cruproc, so I was trying to eliminate all of the rows of interp.df whose coordinates aren't present also in cruproc.

In the example of the rows I presented, only rows 6,9,10 of interp.df would be kept since they have the same coordinates as rows 2,8,7 respectively in the other dataframe. So that the output should be:

1  -97.25  15.75 3.0526965
2  -92.75  15.75 2.6617398
3  -93.25  15.75 2.6521587

I'm pretty new on R, so I would really appreciate any insights on how to do this.


Solution

  • We could use inner_join():

    return all rows from x where there are matching values in y, and all columns from x and y. If there are multiple matches between x and y, all combination of the matches are returned.

    See https://www.rdocumentation.org/packages/dplyr/versions/0.7.8/topics/join

    library(dplyr)
    
    inner_join(interp.df, cruproc, by=c("x", "y")) %>% 
      select(-value)
    
           x     y       pr
    1 -97.25 15.75 3.052697
    2 -92.75 15.75 2.661740
    3 -93.25 15.75 2.652159