Skip to contents

arrange.SpatVector() orders the geometries of a SpatVector by the values of selected columns.

Usage

# S3 method for class 'SpatVector'
arrange(.data, ..., .by_group = FALSE)

Arguments

.data

A SpatVector created with terra::vect().

...

<data-masking> Variables, or functions of variables. Use desc() to sort a variable in descending order.

.by_group

If TRUE, will sort first by grouping variable. Applies to grouped SpatVector only (see group_by.SpatVector()).

Value

A SpatVector object.

terra equivalent

terra::sort()

Methods

Implementation of the generic dplyr::arrange() function for SpatVector class.

Examples


library(terra)
#> terra 1.7.78
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:terra':
#> 
#>     intersect, union
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

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


# Single variable

v %>%
  arrange(desc(iso2))
#>  class       : SpatVector 
#>  geometry    : polygons 
#>  dimensions  : 9, 3  (geometries, attributes)
#>  extent      : 2892687, 3341372, 2017622, 2361600  (xmin, xmax, ymin, ymax)
#>  coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) 
#>  names       :  iso2  cpro       name
#>  type        : <chr> <chr>      <chr>
#>  values      : ES-ZA    49     Zamora
#>                ES-VA    47 Valladolid
#>                ES-SO    42      Soria


# Two variables
v %>%
  mutate(even = as.double(cpro) %% 2 == 0, ) %>%
  arrange(desc(even), desc(iso2))
#>  class       : SpatVector 
#>  geometry    : polygons 
#>  dimensions  : 9, 4  (geometries, attributes)
#>  extent      : 2892687, 3341372, 2017622, 2361600  (xmin, xmax, ymin, ymax)
#>  coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) 
#>  names       :  iso2  cpro     name      even
#>  type        : <chr> <chr>    <chr> <logical>
#>  values      : ES-SO    42    Soria      TRUE
#>                ES-SG    40  Segovia      TRUE
#>                 ES-P    34 Palencia      TRUE


# With new variables
v %>%
  mutate(area_geom = terra::expanse(v)) %>%
  arrange(area_geom)
#>  class       : SpatVector 
#>  geometry    : polygons 
#>  dimensions  : 9, 4  (geometries, attributes)
#>  extent      : 2892687, 3341372, 2017622, 2361600  (xmin, xmax, ymin, ymax)
#>  coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) 
#>  names       :  iso2  cpro     name area_geom
#>  type        : <chr> <chr>    <chr>     <num>
#>  values      : ES-SG    40  Segovia 6.921e+09
#>                 ES-P    34 Palencia 8.042e+09
#>                ES-AV    05    Avila 8.053e+09