library(terra)
f <- system.file("extdata/cyl_temp.tif", package = "tidyterra")
r <- rast(f)
# Slice first 100 cells
r %>%
slice(1:100) %>%
plot()
# Rows
r %>%
slice_rows(1:30) %>%
plot()
# Cols
r %>%
slice_cols(-(20:50)) %>%
plot()
# Spatial sample
r %>%
slice_sample(prop = 0.2) %>%
plot()
# Slice regions
r %>%
slice_colrows(
cols = c(20:40, 60:80),
rows = -c(1:20, 30:50)
) %>%
plot()
# Group wise operation with SpatVectors--------------------------------------
v <- terra::vect(system.file("ex/lux.shp", package = "terra"))
# \donttest{
glimpse(v) %>% autoplot(aes(fill = NAME_1))
gv <- v %>% group_by(NAME_1)
# All slice helpers operate per group, silently truncating to the group size
gv %>%
slice_head(n = 1) %>%
glimpse() %>%
autoplot(aes(fill = NAME_1))
gv %>%
slice_tail(n = 1) %>%
glimpse() %>%
autoplot(aes(fill = NAME_1))
gv %>%
slice_min(AREA, n = 1) %>%
glimpse() %>%
autoplot(aes(fill = NAME_1))
gv %>%
slice_max(AREA, n = 1) %>%
glimpse() %>%
autoplot(aes(fill = NAME_1))
# }
Run the code above in your browser using DataLab