Learn R Programming

tidyterra (version 0.3.0)

slice.Spat: Subset cells/rows/columns/geometries using their positions

Description

slice() 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() and slice_tail() select the first or last cells/geometries.

  • slice_sample() randomly selects cells/geometries.

  • slice_rows() and slice_cols() allow to subset entire rows or columns, of a SpatRaster.

  • slice_colrows() subsets regions of the raster by row and column position of a SpatRaster.

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 SpatRaster
slice(.data, ..., .preserve = FALSE, .keep_extent = FALSE)

# S3 method for SpatVector slice(.data, ..., .preserve = FALSE)

# S3 method for SpatRaster slice_head(.data, ..., n, prop, .keep_extent = FALSE)

# S3 method for SpatVector slice_head(.data, ..., n, prop)

# S3 method for SpatRaster slice_tail(.data, ..., n, prop, .keep_extent = FALSE)

# S3 method for SpatVector slice_tail(.data, ..., n, prop)

# S3 method for SpatRaster slice_min( .data, order_by, ..., n, prop, with_ties = TRUE, .keep_extent = FALSE, na.rm = TRUE )

# S3 method for SpatVector slice_min(.data, order_by, ..., n, prop, with_ties = TRUE)

# S3 method for SpatRaster slice_max( .data, order_by, ..., n, prop, with_ties = TRUE, .keep_extent = FALSE, na.rm = TRUE )

# S3 method for SpatVector slice_max(.data, order_by, ..., n, prop, with_ties = TRUE)

# S3 method for SpatRaster slice_sample( .data, ..., n, prop, weight_by = NULL, replace = FALSE, .keep_extent = FALSE )

# S3 method for SpatVector slice_sample(.data, ..., n, prop, weight_by = NULL, replace = FALSE)

slice_rows(.data, ...)

# S3 method for SpatRaster slice_rows(.data, ..., .keep_extent = FALSE)

slice_cols(.data, ...)

# S3 method for SpatRaster slice_cols(.data, ..., .keep_extent = FALSE)

slice_colrows(.data, ...)

# S3 method for SpatRaster slice_colrows(.data, ..., cols, rows, .keep_extent = FALSE, inverse = FALSE)

Value

A Spat* object of the same class than .data. See Methods.

Arguments

.data

A SpatRaster created with terra::rast() or a SpatVector created with terra::vect().

...

data-masking Integer row values. Provide either positive values to keep, or negative values to drop.

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 Spat* objects

.keep_extent

Should the extent of the resulting SpatRaster be kept? See also terra::trim(), terra::extend().

n, prop

Provide either n, the number of rows, or prop, the proportion of rows to select. If neither are supplied, n = 1 will be used.

If a negative value of n or prop is provided, the specified number or proportion of rows will be removed.

If n is greater than the number of rows in the group (or prop > 1), the result will be silently truncated to the group size. If the proportion of a group size does not yield an integer number of rows, the absolute value of prop*nrow(.data) is rounded down.

order_by

Variable or function of variables to order by.

with_ties

Should ties be kept together? The default, TRUE, may return more rows than you request. Use FALSE to ignore ties, and return the first n rows.

na.rm

Logical, should cells that present a value of NA removed when computing slice_min()/slice_max()?. The default is TRUE.

weight_by

Sampling weights. This must evaluate to a vector of non-negative numbers the same length as the input. Weights are automatically standardised to sum to 1.

replace

Should sampling be performed with (TRUE) or without (FALSE, the default) replacement.

cols, rows

Integer col/row values of the SpatRaster

inverse

If TRUE, .data is inverse-masked to the given selection. See terra::mask().

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

This method relies on the implementation of dplyr::slice() method on the sf package. The result is a SpatVector where the attributes of the selected geometries are preserved.

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 dplyr methods: filter.Spat, mutate.Spat, pull.Spat, relocate.Spat, rename.Spat, select.Spat

Other single table verbs: filter.Spat, mutate.Spat, rename.Spat, select.Spat

Examples

Run this code


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()

Run the code above in your browser using DataLab