-
Notifications
You must be signed in to change notification settings - Fork 0
subsetting
stars
objects are lists of identically shaped arrays, with one array
per variable. This is analogous to the data.frame
which is a list of
identically shaped vectors, with one vector per variable. Note: there
is no requirement that each variable be of the same data type as all of
the others (numeric, character, factor, etc.) Thus is is possible to
perform manipulations on stars
that are analogous to manipulations on
data.frame
objects.
There is a wonderful
vignette that
describes tidyverse
methods for stars
- there is no need to reinvent
the wheel here.
This is actually well described in this vignette but I thought it worth embellishment here.
First we load a toy dataset. Here we make it a 2 variable dataset.
toy <- c(toy_multi(), toy_multi() |> dplyr::mutate(b1.b2.b3.b4.b5 = b1.b2.b3.b4.b5 /500)) |>
rlang::set_names(c("A", "B"))
toy
## stars object with 3 dimensions and 2 attributes
## attribute(s):
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## A 1.000 115.500 234.000 237.000 356.500 500 225
## B 0.002 0.231 0.468 0.474 0.713 1 225
## dimension(s):
## from to offset delta values x/y
## x 1 10 0 1 NULL [x]
## y 1 10 10 -1 NULL [y]
## band 1 5 NA NA b1,...,b5
Below is a table outline different methods for manipulation. Pay close
attention to slicing and the order of arguments to [
which are usually
[attribute/variable, x/lon, y/lat, band/layer]. Note that stars
is
customizable so the order of dimensions, other than attribute/variable
is really use defined.
Action | Subset | Tidy | Outcome |
---|---|---|---|
select attributes(s) | toy[‘A’] |
select(toy, A) |
stars object |
extract entire array | toy[[‘A’]] |
pull(toy, A) |
n-d array |
slice | toy[2,1:3,6:10,2:3] |
select(toy, B) |\> slice(x, 1:3) |\> slice(y, 6:10) |\> slice(band, 2:3 ) |
stars object |
Selection of one or more attributes.
toy['A']
## stars object with 3 dimensions and 1 attribute
## attribute(s):
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## A 1 115.5 234 237 356.5 500 225
## dimension(s):
## from to offset delta values x/y
## x 1 10 0 1 NULL [x]
## y 1 10 10 -1 NULL [y]
## band 1 5 NA NA b1,...,b5
select(toy, A)
## stars object with 3 dimensions and 1 attribute
## attribute(s):
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## A 1 115.5 234 237 356.5 500 225
## dimension(s):
## from to offset delta values x/y
## x 1 10 0 1 NULL [x]
## y 1 10 10 -1 NULL [y]
## band 1 5 NA NA b1,...,b5
Extraction of the array of data for one attribute.
toy[['A']] |> str()
## num [1:10, 1:10, 1:5] 1 2 3 4 5 6 7 8 9 10 ...
## - attr(*, "names")= chr [1:500] "b11" "b12" "b13" "b14" ...
pull(toy, A) |> str()
## num [1:10, 1:10, 1:5] 1 2 3 4 5 6 7 8 9 10 ...
Extraction of slices of the array.
toy[2,1:3, 6:10, 2:3]
## stars object with 3 dimensions and 1 attribute
## attribute(s):
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## B NA NA NA NaN NA NA 30
## dimension(s):
## from to offset delta values x/y
## x 1 3 0 1 NULL [x]
## y 6 10 10 -1 NULL [y]
## band 2 3 NA NA b2, b3
select(toy, B) |> slice(x, 1:3) |> slice(y, 6:10) |> slice(band, 2:3)
## stars object with 3 dimensions and 1 attribute
## attribute(s):
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## B NA NA NA NaN NA NA 30
## dimension(s):
## from to offset delta values x/y
## x 1 3 0 1 NULL [x]
## y 6 10 10 -1 NULL [y]
## band 2 3 NA NA b2, b3