Learn R Programming

terra (version 1.1-4)

values: Get or set cell values

Description

values returns all cell values of a SpatRaster (a matrix), or all the attributes of a SpatVector (a data.frame).

Usage

# S4 method for SpatRaster
values(x, mat=TRUE, dataframe=FALSE, row=1, nrows=nrow(x), col=1, ncols=ncol(x))

# S4 method for SpatRaster,ANY values(x)<-value

# S4 method for SpatRaster,ANY setValues(x, values)

# S4 method for SpatVector values(x)

# S4 method for SpatVector,data.frame values(x)<-value

Arguments

x

SpatRaster or SpatVector

mat

logical. If TRUE, values are returned as a matrix instead of as a vector, except when dataframe is TRUE

dataframe

logical. If TRUE, values are returned as a data.frame instead of as a vector (also if matrix is TRUE)

row

positive integer. Row number to start from, should be between 1 and nrow(x)

nrows

positive integer. How many rows?

col

positive integer. Column number to start from, should be between 1 and ncol(x)

ncols

positive integer. How many columns? Default is the number of columns left after the start column

value

For SpatRaster: matrix or numeric, the length must match the total number of cells (ncell(x) * nlyr(x)), or be a single value. For SpatVector: data.frame or NULL

values

Same as for value

Value

matrix, vector, SpatRaster, or nothing

Details

If x is a SpatRaster:

If matrix=TRUE, a matrix is returned in which the values of each layer are represented by a column (with ncell(x) rows). The values per layer are in cell-order, that is, from top-left, to top-right and then down by row. Use as.matrix for an alternative matrix representation where the number of rows and columns matches that of x, if x has a single layer. If matrix=FALSE, the values are returned as a vector. In cell-order by layer.

If x is a SpatVector: a data.frame

Examples

Run this code
# NOT RUN {
r <- rast(system.file("ex/test.tif", package="terra"))
r
v <- values(r)
values(r) <- v * 10
r

f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
x <- values(v)
x
values(v) <- x[,1:2]
v
# }

Run the code above in your browser using DataLab