[R-sig-Geo] Does stars::st_extract preserve records order

DUTRIEUX Loic Lo|c@DUTRIEUX @end|ng |rom ec@europ@@eu
Wed Jul 27 11:53:43 CEST 2022


Hi everyone,

I'm wondering whether st_extract() from stars package preserves the order of the input sf dataframe.
See the example below; is it a safe approach? Is there perhaps a more "elegant" way to do the same extraction?

library(sf)
library(stars)
library(geodata)
library(magrittr)

# Create sf dataframe of cities
cities <- data.frame(x=c(2.3522, 5.3698, 4.8357),
                     y=c(48.8566, 43.2965, 45.7640),
                     names=c('Paris', 'Marseille', 'Lyon'))
sfdf <- st_as_sf(cities,
                 coords=c('x','y'),
                 crs=st_crs(4326),
                 agr='identity')

# Get raster of monthly average tempature
tavg <- worldclim_tile(var = 'tavg', lon = 15, lat = 45, path = tempdir()) %>%
    st_as_stars()

# Extract monthly avg temperature for cities
tavg_df <- st_extract(tavg, at = sfdf) %>%
    st_as_sf() %>%
    st_drop_geometry()

# Recombine with original sf dataframe
full_df <- cbind(sfdf, tavg_df)


Kind regards,
Loïc



More information about the R-sig-Geo mailing list