slice.Spat {tidyterra} | R Documentation |
Subset cells/rows/columns/geometries using their positions
Description
slice()
methods lets you index cells/rows/columns/geometries by their
(integer) locations. It allows you to select, remove, and duplicate those
dimensions of a Spat*
object.
If you want to slice your SpatRaster
by geographic coordinates use
filter.SpatRaster()
method.
It is accompanied by a number of helpers for common use cases:
-
slice_head()
andslice_tail()
select the first or last cells/geometries. -
slice_sample()
randomly selects cells/geometries. -
slice_rows()
andslice_cols()
allow to subset entire rows or columns, of aSpatRaster
. -
slice_colrows()
subsets regions of theSpatRaster
by row and column position of aSpatRaster
.
You can get a skeleton of your SpatRaster
with the cell, column and row
index with as_coordinates()
.
See Methods for details.
Usage
## S3 method for class 'SpatRaster'
slice(.data, ..., .preserve = FALSE, .keep_extent = FALSE)
## S3 method for class 'SpatVector'
slice(.data, ..., .preserve = FALSE)
## S3 method for class 'SpatRaster'
slice_head(.data, ..., n, prop, .keep_extent = FALSE)
## S3 method for class 'SpatVector'
slice_head(.data, ..., n, prop)
## S3 method for class 'SpatRaster'
slice_tail(.data, ..., n, prop, .keep_extent = FALSE)
## S3 method for class 'SpatVector'
slice_tail(.data, ..., n, prop)
## S3 method for class 'SpatRaster'
slice_min(
.data,
order_by,
...,
n,
prop,
with_ties = TRUE,
.keep_extent = FALSE,
na.rm = TRUE
)
## S3 method for class 'SpatVector'
slice_min(.data, order_by, ..., n, prop, with_ties = TRUE, na_rm = FALSE)
## S3 method for class 'SpatRaster'
slice_max(
.data,
order_by,
...,
n,
prop,
with_ties = TRUE,
.keep_extent = FALSE,
na.rm = TRUE
)
## S3 method for class 'SpatVector'
slice_max(.data, order_by, ..., n, prop, with_ties = TRUE, na_rm = FALSE)
## S3 method for class 'SpatRaster'
slice_sample(
.data,
...,
n,
prop,
weight_by = NULL,
replace = FALSE,
.keep_extent = FALSE
)
## S3 method for class 'SpatVector'
slice_sample(.data, ..., n, prop, weight_by = NULL, replace = FALSE)
slice_rows(.data, ...)
## S3 method for class 'SpatRaster'
slice_rows(.data, ..., .keep_extent = FALSE)
slice_cols(.data, ...)
## S3 method for class 'SpatRaster'
slice_cols(.data, ..., .keep_extent = FALSE)
slice_colrows(.data, ...)
## S3 method for class 'SpatRaster'
slice_colrows(.data, ..., cols, rows, .keep_extent = FALSE, inverse = FALSE)
Arguments
.data |
A |
... |
< The values provided must be either all positive or all negative. Indices beyond the number of rows in the input are silently ignored. See Methods. |
.preserve |
Ignored for |
.keep_extent |
Should the extent of the resulting |
n , prop |
Provide either A negative value of |
order_by |
< |
with_ties |
Should ties be kept together? The default, |
na.rm |
Logical, should cells that present a value of |
na_rm |
Should missing values in |
weight_by |
< |
replace |
Should sampling be performed with ( |
cols , rows |
Integer col/row values of the |
inverse |
If |
Value
A Spat*
object of the same class than .data
. See Methods.
terra equivalent
terra::subset()
, terra::spatSample()
Methods
Implementation of the generic dplyr::slice()
function.
SpatRaster
The result is a SpatRaster
with the crs and resolution of the input and
where cell values of the selected cells/columns/rows are preserved.
Use .keep_extent = TRUE
to preserve the extent of .data
on the output.
The non-selected cells would present a value of NA
.
SpatVector
The result is a SpatVector
where the attributes of the selected
geometries are preserved. If .data
is a
grouped SpatVector
, the operation will be
performed on each group, so that (e.g.) slice_head(df, n = 5)
will select
the first five rows in each group.
See Also
dplyr::slice()
, terra::spatSample()
.
You can get a skeleton of your SpatRaster
with the cell, column and row
index with as_coordinates()
.
If you want to slice by geographic coordinates use filter.SpatRaster()
.
Other single table verbs:
arrange.SpatVector()
,
filter.Spat
,
mutate.Spat
,
rename.Spat
,
select.Spat
,
summarise.SpatVector()
Other dplyr verbs that operate on rows:
arrange.SpatVector()
,
distinct.SpatVector()
,
filter.Spat
Other dplyr methods:
arrange.SpatVector()
,
bind_cols.SpatVector
,
bind_rows.SpatVector
,
count.SpatVector()
,
distinct.SpatVector()
,
filter-joins.SpatVector
,
filter.Spat
,
glimpse.Spat
,
group-by.SpatVector
,
mutate-joins.SpatVector
,
mutate.Spat
,
pull.Spat
,
relocate.Spat
,
rename.Spat
,
rowwise.SpatVector()
,
select.Spat
,
summarise.SpatVector()
Examples
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"))
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))