Here is an example of a problem I am attempting to solve and implements in a much larger database:
I have a sparse grid of points across the new world, with lat and long defined as below.
LAT<-rep(-5:5*10, 5)
LON<-rep(seq(-140, -60, by=20), each=11)
I know the color of some points on my grid
COLOR<-(c(NA,NA,NA,"black",NA,NA,NA,NA,NA,"red",NA,NA,"green",NA,"blue","blue",NA,"blue",NA,NA,"yellow",NA,NA,"yellow",NA+
NA,NA,NA,"blue",NA,NA,NA,NA,NA,NA,NA,"black",NA,"blue","blue",NA,"blue",NA,NA,"yellow",NA,NA,NA,NA,"red",NA,NA,"green",NA,"blue","blue"))
data<-as.data.frame(cbind(LAT,LON,COLOR))
What I want to do is replace the NA values in COLOR with the color that is closeset (in distance) to that point. In the actual implementation, I am not worried too much with ties, but I suppose it is possible (I could probably fix those by hand).
Thanks
Yup.
First, make your data frame with
data.frameor things all get coerced to characters:Split the data frame up – you could probably do this in one go but this makes things a bit more obvious:
Now insert the replacement colours directly into the
datadataframe:Note however that distance is being computed using pythagoras geometry on lat-long, which isn’t true because the earth isn’t flat. You might have to transform your coordinates to something else first.