Search code examples
rdataframecoordinatesdata-extraction

How to extract data from the points closer to required location in R?


I have two data.frame. FakeData has the Long & Lat along with data while ExCor has Long & Lat of the points that I would like to have the data extracted from FakeData. ExCor should only extract data from the closest points. Here is some sample data to start with

rm(list = ls())

DF1 = data.frame(t(data.frame(Grids = 1:5, Long = runif(5, min = 5, max = 10), Lat = runif(5, min = 2, max = 3))))
DF2 = data.frame(X1 = runif(1095, 0.5,1), X2 = runif(1095, 1.5,2), X3 = runif(1095, 0.5,1.5), X4 = runif(1095, 0.5,6), X5 = runif(1095, 0.5,15))
FakeData = rbind(DF1, DF2)

ExCor = data.frame(t(data.frame(Long = runif(3, 7, 9), Lat = runif(3, 2,3))))

Any thoughts are welcome.


Solution

  • One of many ways to achieve this is to use the st_nearest_feature predicate within sf::st_join. First, though, you need to rejig your data structure so that it is more compatible with the packages that work with spatial geometries.

    library(sf)
    
    # Reshape datasets so that coords and attributes are columns, not rows
    ExCor <- setNames(do.call(rbind.data.frame, ExCor), row.names(ExCor))
    FakeData <- setNames(do.call(rbind.data.frame, FakeData), row.names(FakeData))
    
    # Convert to sf objects
    FakeData_sf <- st_as_sf(FakeData, coords=c('Long', 'Lat'))
    ExCor_sf <- st_as_sf(ExCor, coords=c('Long', 'Lat'))
    
    # Spatial join nearest feature
    newdat <- st_join(ExCor_sf, FakeData_sf, join=st_nearest_feature)
    

    You can then write out to a .csv again if you want...

    st_write(newdat, 'newdat.csv', layer_options='GEOMETRY=AS_XY')