I've got a grid/raster in R that is represented as centroids of the grid instead of polygons. I am trying to merge that with points.

nc <- st_read(system.file("shape/nc.shp", package="sf"))
grid_10000 <- st_make_grid(nc, cellsize = c(10000, 10000)) %>% 
  st_sf(grid_id = 1:length(.),grid_prop = sample(c("one","two"),length(.),replace=T))
grid_centroids <- st_centroid(grid_50) %>% cbind(st_coordinates(.))
pts <- st_sample(nc, size = 5) %>% st_sf
# I can join the points easily like this
joined <- st_join(pts,grid_10000)

Now without grid_10000, how could I see which point gets which parameter simply based on the centroids. A method that could make a grid from the centroids would be best. The distances are unknown too. And 'sf' solution would be preferred.

  • Your last line, joined <- (pts,grid_10000) isn't valid R - did you mean c(pts, grid_10000)?
    – Spacedman
    Aug 19, 2018 at 9:10

1 Answer 1


Ok, made a workaround, by estimating the distance from two adjacent centroids and joining the centroids with the newly made grid. But still thinking there ought to be an easier way.

grid_new <- st_make_grid(nc, cellsize = 
    as.numeric(st_distance(grid_centroids[1:2,])[,2][1]))) %>% 
    st_sf(grid_id = 1:length(.))
grid_with_data_from_centroids <- st_join(grid_new,grid_centroids)
joined <- st_join(pts,grid_with_data_from_centroids)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.