Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging two data frame with the coordinate in r [duplicate]

Tags:

r

I have two data set, one with only world grids "Ggrid" - (LON- -179.875, 179.875 and LAT- -89.875, 89.875. Making a total of 1036800 ) and the other with world grids and oxygen data at different depth "1JAN" - ( LON- -79.5, 179.5 and LAT- -89.5, 89.85.). I would like to merge this data by the World grids so that I would have a total of 1036800( 720 by 1440) and the cell without data should be NA.

And I tried this;

> ENV1<-read.csv('1JAN.csv')
> Ggrid<-read.csv('Ggrid.csv')
> head(Ggrid)
       LON     LAT
1 -179.875 -89.875
2 -179.875 -89.625
3 -179.875 -89.375
4 -179.875 -89.125
5 -179.875 -88.875
6 -179.875 -88.625

> ENV1 <- ENV1[,1:7]
> head(ENV1)
    LAT    LON   X0    X5   X10
1 -77.5 -178.5 8.28    NA    NA
2 -77.5 -174.5   NA    NA    NA
3 -77.5 -170.5 7.96 7.991 8.000
4 -77.5 -167.5 8.08 8.090 8.100
5 -77.5 -165.5 8.09 8.154 8.180
6 -77.5 -163.5 8.93 8.923 8.905


> m2 <- merge(Ggrid, ENV1, by = c("LAT","LON")all.x=T)


1   NA   NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
2   NA   NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
3   NA   NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
4   NA   NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
5   NA   NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA
6   NA   NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA

The problem is the coordinates do not match and all points could be located on Ggrid. I asked the question earlier and answers were given if the coordinate match but in this new case, the coordinates are different.

ENV1 looks like this:

LON LAT X0  X5  X10
-77.5   -178.5  8.28    NA  NA
-77.5   -178    7.28    NA  NA
-77.5   -177.5  8.06    NA  NA
-77.5   -177    7.65    7.43    NA
-77.5   -176.5  7.54    7.32    NA
-77.5   -176    7.43    7.21    NA
-77.5   -175.5  7.32    7.1 7.28
-77.5   -175    7.21    6.99    8.06
-77.5   -174.5  7.1 6.88    7.65
-77.5   -174    6.99    7.43    7.54
-77.5   -173.5  6.88    7.32    6.88
-77.5   -173    6.77    7.21    7.28
-77.5   -172.5  6.66    7.28    7.28

after merging with COO; it should look like this;

LON LAT X0  X5  X10
-77.675 -178.875    8.28    NA  NA
-77.675 -178.625    7.28    NA  NA
-77.675 -177.375    8.06    NA  NA
-77.675 -177.125    7.65    7.43    NA
-77.675 -176.875    7.54    7.32    NA
-77.675 -176.625    7.43    7.21    NA
-77.675 -175.375    7.32    7.1 7.28
-77.675 -175.125    7.21    6.99    8.06
-77.675 -174.875    7.1 6.88    7.65
-77.675 -174.625    6.99    7.43    7.54
-77.675 -173.375    6.88    7.32    6.88
-77.675 -173.125    6.77    7.21    7.28
-77.675 -172.875    6.66    7.28    7.28

Hope this help further. Thanks

like image 251
user5545418 Avatar asked Feb 09 '23 06:02

user5545418


1 Answers

I think you're looking for a left join. Try m2 <- merge(Ggrid, ENV1, by=c("LAT", "LON"), all.x=T)

like image 156
Ricky Avatar answered Feb 16 '23 02:02

Ricky