./data/ folder.
sf::read_sf() function to load the data. Sampling is straightforward: Apply the sf::st_sample to the loaded shapefile, but make sure to apply the sf::st_as_sf() function afterward to receive a full-fledged data table (with a geometry column only).
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
cologne <-
sf::read_sf("../data/cologne.shp")
cologne_50_points <-
cologne %>%
sf::st_sample(50) %>%
sf::st_as_sf()
immigrants_cologne.tif and inhabitants_cologne.tif files in the ./data/ folder.
library(raster)
## Loading required package: sp
##
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
##
## select
immigrant_rates <-
raster::raster("../data/immigrants_cologne.tif") * 100 /
raster::raster("../data/inhabitants_cologne.tif")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
immigrant_rates_at_point <-
raster::extract(immigrant_rates, cologne_50_points)
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
## Warning in showSRID(SRS_string, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum European Terrestrial Reference System 1989 in CRS
## definition
# There may be a lot of missing values.
buffer = 500. In that case, you should also set a descriptive statistics function, e.g., with the option fun = mean.
immigrant_rates_500m_buffer <-
raster::extract(immigrant_rates, cologne_50_points, buffer = 500, fun = mean)
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
## Warning in showSRID(SRS_string, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum European Terrestrial Reference System 1989 in CRS
## definition
# There may be a lot of missing values.