Learn R Programming

tidyterra (version 0.1.0)

as_tibble: Coerce a Spat* object to data frames

Description

as_tibble() method for SpatRaster and SpatVector.

Usage

# S3 method for SpatRaster
as_tibble(x, ..., xy = FALSE, na.rm = FALSE, .name_repair = "unique")

# S3 method for SpatVector as_tibble(x, ..., .name_repair = "unique")

Arguments

x

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

...

Arguments passed on to terra::as.data.frame()

xy

logical. If TRUE, the coordinates of each raster cell are included

na.rm

logical. If TRUE, cells that have a NA value in at least one layer are removed

.name_repair

Treatment of problematic column names:

  • "minimal": No name repair or checks, beyond basic existence,

  • "unique": Make sure names are unique and not empty,

  • "check_unique": (default value), no name repair, but check they are unique,

  • "universal": Make the names unique and syntactic

  • a function: apply custom name repair (e.g., .name_repair = make.names for names in the style of base R).

  • A purrr-style anonymous function, see rlang::as_function()

Value

A tibble.

terra equivalent

terra::as.data.frame()

Methods

Implementation of the generic tibble::as_tibble() function.

SpatRaster

[Questioning] The tibble is returned with an attribute including the crs of the initial object in WKT format (see pull_crs()).

About layer names

When coercing SpatRaster objects to data frames, x and y names are reserved for geographic coordinates of each cell of the raster. It should be also noted that terra allows layers with duplicated names.

In the process of coercing a SpatRaster to a tibble, tidyterra may rename the layers of your SpatRaster for overcoming this issue. Specifically, layers may be renamed on the following cases:

  • Layers with duplicated names

  • When coercing to a tibble, if xy = TRUE, layers named x or y would be renamed.

  • When working with tidyverse methods (i.e. filter.SpatRaster()), the latter would happen as well.

tidyterra would display a message informing of the changes on the names of the layer.

See Also

tibble::as_tibble(), terra::as.data.frame()

Coercing objects: as_coordinates(), as_spatraster()

Examples

Run this code
# NOT RUN {
library(terra)
# SpatRaster
f <- system.file("extdata/cyl_temp.tif", package = "tidyterra")
r <- rast(f)

as_tibble(r, na.rm = TRUE)

as_tibble(r, xy = TRUE)

# SpatVector

f <- system.file("extdata/cyl.gpkg", package = "tidyterra")
v <- vect(f)

as_tibble(v)

# }

Run the code above in your browser using DataLab