library(magrittr)
library(ggplot2)
library(sf)
tt <- read_sf(path, "USA_adm1")
# subset some states to make it plot faster
tt1 <- tt[tt$NAME_1 %in% c("South Dakota", "Wyoming",
"Nebraska", "Iowa"), ]
EDIT - I add another version for this operation, inspired by a very important comment made by spacedman. This would save some computing time, in particular form datasets with multiple points/polygons or complex geometries.
pnts <- data.frame(
"x" = c(-105.08798, -99.61295, -96.22951,
-92.35393, -96.59861, -101.45846, -106.87197),
"y" = c(43.27392, 43.48426, 43.05443, 43.04529,
43.14589, 42.80751, 44.22843))
# create a points collection
pnts_sf <- do.call("st_sfc",c(lapply(1:nrow(pnts),
function(i) {st_point(as.numeric(pnts[i, ]))}), list("crs" = 4326)))
pnts_trans <- st_transform(pnts_sf, 2163) # apply transformation to pnts sf
tt1_trans <- st_transform(tt1, 2163) # apply transformation to polygons sf
# intersect and extract state name
pnts$region <- apply(st_intersects(tt1_trans, pnts_trans, sparse = FALSE), 2,
function(col) {
tt1_trans[which(col), ]$NAME_1
})