Skip to contents

Extract subsets of an object from a class derived from 'VirtualArray' similarly to a regular array.

Usage

# S4 method for VirtualArray
subset(x, i, j, ..., oneDim = FALSE, drop = TRUE)

Arguments

x

VirtualArray-class object.

i

subscript of the first dimension(rows) or vector-like subsetting.

j

subscript of the second dimension (columns).

...

subscript of additional dimensions.

oneDim

logical In case of multidimensional VirtualArrays, setting oneDim to TRUE allows the application of one dimensional subscripts.

drop

logical in case the result of subsetting is a single element, should the \codeVirtualArray wrapper be dropped?

Value

Either the same class as x, or the class that forms the element of the VirtualArray.

Examples

ex <- rastex()
# first 4
subset(ex, i=1:4)
#> class         : RasterArray
#> Element properties: 
#> - class       : SpatRaster
#> - dimensions  : 180, 360 (nrow, ncol)
#> - resolution  : 1, 1 (x, y)
#> - extent      : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> - coord.ref.  : WGS 84
#> Array properties: 
#> - dimensions  : 4  (vector)
#> - num. layers : 4
#> - missing     : 0
#> - proxy:
#>          a         b         c         d 
#> "layer_1" "layer_2" "layer_3" "layer_4" 
#> 
# missing at the end
subset(ex, i=1:12)
#> class         : RasterArray
#> Element properties: 
#> - class       : SpatRaster
#> - dimensions  : 180, 360 (nrow, ncol)
#> - resolution  : 1, 1 (x, y)
#> - extent      : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> - coord.ref.  : WGS 84
#> Array properties: 
#> - dimensions  : 12  (vector)
#> - num. layers : 6
#> - missing     : 6
#> - proxy:
#>          a         b         c         d         e         f      <NA>      <NA> 
#> "layer_1" "layer_2" "layer_3" "layer_4" "layer_5" "layer_6"        NA        NA 
#>      <NA>      <NA>      <NA>      <NA> 
#>        NA        NA        NA        NA 
#> 
# character subscript
subset(ex, i=c("a", "b"))
#> class         : RasterArray
#> Element properties: 
#> - class       : SpatRaster
#> - dimensions  : 180, 360 (nrow, ncol)
#> - resolution  : 1, 1 (x, y)
#> - extent      : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> - coord.ref.  : WGS 84
#> Array properties: 
#> - dimensions  : 2  (vector)
#> - num. layers : 2
#> - missing     : 0
#> - proxy:
#>          a         b 
#> "layer_1" "layer_2" 
#> 
# logical subscript
subs <- rep(TRUE, length(ex))
subs[1] <- FALSE # remove first
subset(ex, i= subs)
#> class         : RasterArray
#> Element properties: 
#> - class       : SpatRaster
#> - dimensions  : 180, 360 (nrow, ncol)
#> - resolution  : 1, 1 (x, y)
#> - extent      : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> - coord.ref.  : WGS 84
#> Array properties: 
#> - dimensions  : 5  (vector)
#> - num. layers : 5
#> - missing     : 0
#> - proxy:
#>          b         c         d         e         f 
#> "layer_2" "layer_3" "layer_4" "layer_5" "layer_6" 
#> 
# no drop
subset(ex, i=1, drop=FALSE)
#> class         : RasterArray
#> Element properties: 
#> - class       : SpatRaster
#> - dimensions  : 180, 360 (nrow, ncol)
#> - resolution  : 1, 1 (x, y)
#> - extent      : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> - coord.ref.  : WGS 84
#> Array properties: 
#> - dimensions  : 1  (vector)
#> - num. layers : 1
#> - missing     : 0
#> - proxy:
#>          a 
#> "layer_1" 
#>