
Coerce a SpatVector or SpatRaster object to data frames
      Source: R/as_tibble-Spat.R
      as_tibble.Spat.Rdas_tibble() methods for SpatRaster and SpatVector objects.
Arguments
- x
 A
SpatRastercreated withterra::rast()or aSpatVectorcreated withterra::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 aNAvalue in at least one layer are removed. If the argument is set toNAonly cells that haveNAvalues in all layers 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 areunique."universal": Make the namesuniqueand syntactic.a function: apply custom name repair (e.g.,
.name_repair = make.namesfor names in the style of base R).A purrr-style anonymous function, see
rlang::as_function().
- geom
 character or NULL. If not NULL, either "WKT" or "HEX", to get the geometry included in Well-Known-Text or hexadecimal notation. If
xhas point geometry, it can also be "XY" to add the coordinates of each point
Value
A tibble.
terra equivalent
Methods
Implementation of the generic tibble::as_tibble() function.
SpatRaster and SpatVector
The tibble is returned with an attribute including the crs of the initial
object in WKT format (see pull_crs()).
About layer/column names
When coercing SpatRaster objects to data frames, x and y names are
reserved for geographic coordinates of each cell of the SpatRaster 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 namedxorywould 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.
The same issue happens for SpatVector with names geometry (when
geom = c("WKT", "HEX")) and x, y (when geom = "XY"). These are
reserved names representing the geometry of the SpatVector (see
terra::as.data.frame()). If geom is not NULL then the logic described
for SpatRaster would apply as well for the columns of the SpatVector.
See also
tibble::as_tibble(), terra::as.data.frame()
Coercing objects:
as_coordinates(),
as_sf(),
as_spatraster(),
as_spatvector(),
fortify.Spat,
tidy.Spat
Examples
library(terra)
# SpatRaster
f <- system.file("extdata/cyl_temp.tif", package = "tidyterra")
r <- rast(f)
as_tibble(r, na.rm = TRUE)
#> # A tibble: 6,522 × 3
#>    tavg_04 tavg_05 tavg_06
#>      <dbl>   <dbl>   <dbl>
#>  1    3.18    6.79    11.1
#>  2    5.30    8.73    12.7
#>  3    4.59    8.05    12.1
#>  4    6.38    9.72    13.6
#>  5    4.05    7.58    11.8
#>  6    2.90    6.54    10.9
#>  7    5.05    8.49    12.5
#>  8    2.58    6.17    10.5
#>  9    9.30   12.5     15.4
#> 10    9.84   13.0     15.9
#> # ℹ 6,512 more rows
as_tibble(r, xy = TRUE)
#> # A tibble: 10,266 × 5
#>           x        y tavg_04 tavg_05 tavg_06
#>       <dbl>    <dbl>   <dbl>   <dbl>   <dbl>
#>  1 -610395. 4618746.      NA      NA      NA
#>  2 -606513. 4618746.      NA      NA      NA
#>  3 -602632. 4618746.      NA      NA      NA
#>  4 -598751. 4618746.      NA      NA      NA
#>  5 -594870. 4618746.      NA      NA      NA
#>  6 -590988. 4618746.      NA      NA      NA
#>  7 -587107. 4618746.      NA      NA      NA
#>  8 -583226. 4618746.      NA      NA      NA
#>  9 -579345. 4618746.      NA      NA      NA
#> 10 -575463. 4618746.      NA      NA      NA
#> # ℹ 10,256 more rows
# SpatVector
f <- system.file("extdata/cyl.gpkg", package = "tidyterra")
v <- vect(f)
as_tibble(v)
#> # A tibble: 9 × 3
#>   iso2  cpro  name      
#>   <chr> <chr> <chr>     
#> 1 ES-AV 05    Avila     
#> 2 ES-BU 09    Burgos    
#> 3 ES-LE 24    Leon      
#> 4 ES-P  34    Palencia  
#> 5 ES-SA 37    Salamanca 
#> 6 ES-SG 40    Segovia   
#> 7 ES-SO 42    Soria     
#> 8 ES-VA 47    Valladolid
#> 9 ES-ZA 49    Zamora