0

So, I have a very large of data rows (~10^5); each row contains a latitude, longitude and a classification within the KG climate classification system. I wrote the following code which should be able to take an arbitrary pair of longitude-latitude and return the row number which most closely matches. It simply does not work. I have looked online over quite a few questions dealing with Index-and-Match in R but nothing quite does the trick.

    KGlookup <- function(longitude, latitude)
{
  lat = 0.5*round(latitude*2, 0) + 0.25
  lon = 0.5*round(longitude*2, 0) + 0.25 
  lonlat = c(lon, lat)
  lonlat <- as.data.frame(lonlat)
  tada = match(lonlat, pvpstry)
  return(tada)
}

Thanks in advance!

Edit: to respond to a comment:


> dput(pvpstry[1:50,])
structure(list(V1 = c(-179.75, -179.25, -178.75, -178.25, -177.75, 
-177.25, -176.75, -176.25, -175.75, -175.25, -174.75, -174.25, 
-173.75, -173.25, -172.75, -172.25, -171.75, -171.25, -170.75, 
-170.25, -169.75, -169.25, -168.75, -168.25, -167.75, -167.25, 
-166.75, -166.25, -165.75, -165.25, -164.75, -164.25, -163.75, 
-163.25, -162.75, -162.25, -161.75, -161.25, -160.75, -160.25, 
-159.75, -159.25, -158.75, -158.25, -157.75, -157.25, -156.75, 
-156.25, -155.75, -155.25), V2 = c(-89.75, -89.75, -89.75, -89.75, 
-89.75, -89.75, -89.75, -89.75, -89.75, -89.75, -89.75, -89.75, 
-89.75, -89.75, -89.75, -89.75, -89.75, -89.75, -89.75, -89.75, 
-89.75, -89.75, -89.75, -89.75, -89.75, -89.75, -89.75, -89.75, 
-89.75, -89.75, -89.75, -89.75, -89.75, -89.75, -89.75, -89.75, 
-89.75, -89.75, -89.75, -89.75, -89.75, -89.75, -89.75, -89.75, 
-89.75, -89.75, -89.75, -89.75, -89.75, -89.75)), row.names = c(NA, 
50L), class = "data.frame")
> 


Abed
  • 183
  • 7
  • Hi Abed, could you provide a sample of the very large data with `dput(pvpstry[1:50,])`? You may need to change `pvpstry` to the actual name of the data object if you've attached it. – Ian Campbell Mar 30 '20 at 16:26
  • Hello Ian. I have gotten back to you on that. – Abed Mar 30 '20 at 16:36
  • `match` works on vectors not data.frames. Also, do [this](https://stackoverflow.com/questions/27442506/find-nearest-points-of-latitude-and-longitude-from-different-data-sets-with-diff) post or [this one](https://stackoverflow.com/questions/21977720/r-finding-closest-neighboring-point-and-number-of-neighbors-within-a-given-rad) help? – Rui Barradas Mar 30 '20 at 16:44

1 Answers1

0

One approach would be to use logical comparisons.

KGlookup <- function(longitude, latitude)
{
  lat = 0.5*round(latitude*2, 0) + 0.25
  lon = 0.5*round(longitude*2, 0) + 0.25 
  tada = which(pvpstry[,1] == lon & pvpstry[,2] == lat)
  return(tada)
}
KGlookup(-172.25,-89.75)
# [1] 17
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57