library(terra)
f <- system.file("extdata/cyl_temp.tif", package = "tidyterra")
r <- rast(f) %>% select(tavg_04)
plot(r)
# Filter temps
r_f <- r %>% filter(tavg_04 > 11.5)
# Extent is kept
plot(r_f)
# Filter temps and extent
r_f2 <- r %>% filter(tavg_04 > 11.5, .keep_extent = FALSE)
# Extent has changed
plot(r_f2)
# Filter by geographic coordinates
r2 <- project(r, "epsg:4326")
r2 %>% plot()
r2 %>%
filter(
x > -4,
x < -2,
y > 42
) %>%
plot()
Run the code above in your browser using DataLab