Learn R Programming

terra (version 1.7-55)

split: Split

Description

Split a SpatRaster by layer, or a SpatVector by attributes. You can also split the geometry of a polygon SpatVector with another SpatVector.

Usage

# S4 method for SpatRaster,ANY
split(x, f)

# S4 method for SpatVector,ANY split(x, f)

# S4 method for SpatVector,SpatVector split(x, f)

Value

list or SpatVector

Arguments

x

SpatRaster or SpatVector

f

If x is a SpatRaster: a vector of the length nlyr(x). If x is a SpatVector: a field (variable) name or a vector of the same length as x; or, if x is a SpatVector of polygons, a SpatVector of lines or polygons to split the polygon geometries

Examples

Run this code

## split layers
s <- rast(system.file("ex/logo.tif", package="terra"))   
y <- split(s, c(1,2,1))
sds(y)

## split attributes
v <- vect(system.file("ex/lux.shp", package="terra"))
x <- split(v, "NAME_1")

## split geometries
v <- v[1:5,]
line <- vect(matrix(c(5.79, 6.22, 5.75, 6.1, 5.8, 
	50.14, 50.05, 49.88, 49.85, 49.71), ncol=2), "line")
s <- split(v, line)

Run the code above in your browser using DataLab