Skip to contents

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. If the argument is set to NA only cells that have NA values 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 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.

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,672 × 3
#>    tavg_04 tavg_05 tavg_06
#>      <dbl>   <dbl>   <dbl>
#>  1    4.89    8.25    12.6
#>  2    3.90    7.37    11.8
#>  3    3.92    7.39    11.9
#>  4    4.63    7.99    12.4
#>  5    4.79    8.16    12.5
#>  6    4.20    7.63    12.1
#>  7    3.79    7.33    11.9
#>  8    2.82    6.44    11.1
#>  9    3.66    7.23    11.8
#> 10    4.23    7.72    12.1
#> # … with 6,662 more rows

as_tibble(r, xy = TRUE)
#> # A tibble: 10,324 × 5
#>           x        y tavg_04 tavg_05 tavg_06
#>       <dbl>    <dbl>   <dbl>   <dbl>   <dbl>
#>  1 2895511. 2360761.      NA      NA      NA
#>  2 2899367. 2360761.      NA      NA      NA
#>  3 2903224. 2360761.      NA      NA      NA
#>  4 2907081. 2360761.      NA      NA      NA
#>  5 2910937. 2360761.      NA      NA      NA
#>  6 2914794. 2360761.      NA      NA      NA
#>  7 2918651. 2360761.      NA      NA      NA
#>  8 2922507. 2360761.      NA      NA      NA
#>  9 2926364. 2360761.      NA      NA      NA
#> 10 2930220. 2360761.      NA      NA      NA
#> # … with 10,314 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    Ávila     
#> 2 ES-BU 09    Burgos    
#> 3 ES-LE 24    León      
#> 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