0

I want to append data points which are within the geometry of a sf object. Wonder to know what is the approach to accomplish this with sf package. Any hints, please.

library(tidyverse)
library(maps)
library(sf)
us_map <- 
    map_data('state') %>%
    st_as_sf(coords = c("long", "lat"))

set.seed(12345)
df1 <- 
  tibble(
    long = runif(n = 1000, min = -130, max = -50)
  , lat  = runif(n = 1000, min = 20, max = 60)
  , y    = runif(n = 1000, min = 0, max = 500)
    ) %>%
    st_as_sf(coords = c("long", "lat"))

Data1 <- 
    sf::st_join(
        x       = us_map
      , y        = df1
      , join    = st_intersects
    #  , FUN
      , suffix  = c(".x", ".y")
    #  , ...
      , left    = TRUE
      , largest = FALSE
      )

Data <-
  Data1 %>%
  mutate(yf = if_else(condition = between(x = y, left = 0, right = 200), true = "0-200", false = "200-500", missing = NULL))


plot(Data$geometry)
plot(df1$geometry, add = TRUE)
plot(Data["yf"])

enter image description here

MYaseen208
  • 22,666
  • 37
  • 165
  • 309
  • Are you looking for a point in polygon thingy? https://gis.stackexchange.com/questions/133625/checking-if-points-fall-within-polygon-shapefile – Roman Luštrik Apr 28 '19 at 09:45
  • 2
    What do you want to append the data to? – william3031 Apr 29 '19 at 03:48
  • related? https://stackoverflow.com/questions/52937483/r-fitting-a-grid-over-a-city-map-and-inputting-data-into-grid-squares or https://gis.stackexchange.com/questions/282750/identify-polygon-containing-point-with-r-sf-package – user63230 Aug 22 '19 at 14:42

0 Answers0