Generate a data frame version of any raster object. Use the arguments 'cell', 'dim', 'split_date' and 'value' to control the columns that are included in the output.

# S3 method for BasicRaster
as_tibble(
  x,
  cell = TRUE,
  dim = nlayers(x) > 1L,
  value = TRUE,
  split_date = FALSE,
  xy = FALSE,
  ...
)

Arguments

x

a RasterLayer, RasterStack or RasterBrick

cell

logical to include explicit cell number

dim

logical to include slice index

value

logical to return the values as a column or not

split_date

logical to split date into components

xy

logical to include the x and y centre coordinate of each cell

...

unused

Value

a data frame (tibble) with columns:

  • cellvalue the actual value of the raster cell

  • cellindex the index of the cell (numbered from 1 to ncell() in the raster way).

Columns cellindex or cellvalue may be omitted if either or both of cell and/or value are FALSE, respectively

Other columns might be included depending on the properties of the raster and the arguments to the function:

  • year,month,day if split_date is TRUE

  • x,y if xy is TRUE

  • dimindex if the input has more than 1 layer and dim is TRUE.

Details

If the raster has only one layer, the slice index is not added. Use 'dim = FALSE' to not include the slice index value.

Examples

## basic data frame version of a basic raster
as_tibble(raster::raster(volcano))
#> # A tibble: 5,307 × 2
#>    cellvalue cellindex
#>        <dbl>     <int>
#>  1       100         1
#>  2       100         2
#>  3       101         3
#>  4       101         4
#>  5       101         5
#>  6       101         6
#>  7       101         7
#>  8       100         8
#>  9       100         9
#> 10       100        10
#> # ℹ 5,297 more rows

## data frame with time column since raster has that set
r <- raster::raster(volcano)
br <- raster::brick(r, r)
as_tibble(raster::setZ(br, Sys.Date() + 1:2), cell = TRUE)
#> # A tibble: 10,614 × 3
#>    cellvalue cellindex dimindex  
#>        <dbl>     <int> <date>    
#>  1       100         1 2024-01-11
#>  2       100         2 2024-01-11
#>  3       101         3 2024-01-11
#>  4       101         4 2024-01-11
#>  5       101         5 2024-01-11
#>  6       101         6 2024-01-11
#>  7       101         7 2024-01-11
#>  8       100         8 2024-01-11
#>  9       100         9 2024-01-11
#> 10       100        10 2024-01-11
#> # ℹ 10,604 more rows