Skip to contents

A method to extract a random sample of members from a list, a collection of spectra or a spectrum object containing multiple spectra in long form.

Usage

pull_sample(x, size, ...)

# Default S3 method
pull_sample(x, size, ...)

# S3 method for class 'list'
pull_sample(
  x,
  size = 1,
  replace = FALSE,
  keep.order = TRUE,
  method = "random",
  simplify = FALSE,
  ...
)

# S3 method for class 'generic_spct'
pull_sample(
  x,
  size = 1,
  replace = FALSE,
  keep.order = TRUE,
  method = "random",
  ...
)

# S3 method for class 'generic_mspct'
pull_sample(
  x,
  size = 1,
  replace = FALSE,
  keep.order = TRUE,
  method = "random",
  recursive = FALSE,
  simplify = FALSE,
  ...
)

Arguments

x

An R object possibly containing multiple spectra or other components.

size

integer The number of spectra to extract, if available.

...

currently ignored.

replace

logical Sample with or without replacement.

keep.order

logical Return the spectra ordered as in x or in random order.

method

character Use "random" for random sampling or "equal.steps" for systematic sampling.

simplify

logical If size = 1, and x is a collection return the spectrum object instead of a collection with it as only member.

recursive

logical If x is a collection, expand or not member spectra containing multiple spectra in long form into individual members before sampling.

Value

If x is an spectrum object, such as a "filter_spct" object, the returned object is of the same class but in most cases containing fewer spectra in long form than x. If x is a collection of spectrum objecta, such as a "filter_mspct" object, the returned object is of the same class but in most cases containing fewer member spectra than x.

Details

This function calls sample() to generate a random set of indexes, or a regular sequence, and uses it to extract members from lists or collections. Method "equal.steps" is most useful for time series of spectra. With recursive = TRUE lists and list-like collections are first flattened, and with recursive = FALSE sampling is applied to the topmost level only.

See also

See sample for the method used for the sampling.

Examples

a.list <- as.list(letters)
names(a.list) <- LETTERS
set.seed(12345678)
pull_sample(a.list, size = 8)
#> $D
#> [1] "d"
#> 
#> $K
#> [1] "k"
#> 
#> $L
#> [1] "l"
#> 
#> $N
#> [1] "n"
#> 
#> $O
#> [1] "o"
#> 
#> $P
#> [1] "p"
#> 
#> $R
#> [1] "r"
#> 
#> $Y
#> [1] "y"
#> 
pull_sample(a.list, size = 7, method = "equal.steps")
#> $A
#> [1] "a"
#> 
#> $D
#> [1] "d"
#> 
#> $G
#> [1] "g"
#> 
#> $J
#> [1] "j"
#> 
#> $M
#> [1] "m"
#> 
#> $P
#> [1] "p"
#> 
#> $S
#> [1] "s"
#> 
pull_sample(a.list, size = 8, keep.order = FALSE)
#> $M
#> [1] "m"
#> 
#> $B
#> [1] "b"
#> 
#> $J
#> [1] "j"
#> 
#> $Y
#> [1] "y"
#> 
#> $Q
#> [1] "q"
#> 
#> $R
#> [1] "r"
#> 
#> $K
#> [1] "k"
#> 
#> $H
#> [1] "h"
#> 
pull_sample(a.list, size = 8, replace = TRUE)
#> $A
#> [1] "a"
#> 
#> $C
#> [1] "c"
#> 
#> $G
#> [1] "g"
#> 
#> $G.copy1
#> [1] "g"
#> 
#> $I
#> [1] "i"
#> 
#> $I.copy1
#> [1] "i"
#> 
#> $N
#> [1] "n"
#> 
#> $W
#> [1] "w"
#> 
pull_sample(a.list, size = 8, replace = TRUE, keep.order = FALSE)
#> $Y
#> [1] "y"
#> 
#> $J
#> [1] "j"
#> 
#> $G
#> [1] "g"
#> 
#> $D
#> [1] "d"
#> 
#> $U
#> [1] "u"
#> 
#> $N
#> [1] "n"
#> 
#> $S
#> [1] "s"
#> 
#> $P
#> [1] "p"
#> 
pull_sample(a.list, size = 1)
#> $R
#> [1] "r"
#> 
pull_sample(a.list, size = 1, simplify = TRUE)
#> [1] "v"

set.seed(12345678)
pull_sample(sun_evening.spct, 2)
#> Object: source_spct [3,186 x 3]
#> containing 2 spectra in long form
#> Wavelength range 290-1000 nm, step 0.34-0.47 nm 
#> Label: cosine.hour.9 
#> time.04 measured on 2023-06-12 18:41:00.768459 UTC
#> time.05 measured on 2023-06-12 18:42:00.769065 UTC 
#> Measured at 60.227 N, 24.018 E; Viikki, Helsinki, FI 
#> Variables:
#>  w.length: Wavelength [nm]
#>  s.e.irrad: Spectral energy irradiance [W m-2 nm-1] 
#> --
#> # A tibble: 3,186 × 3
#>    w.length s.e.irrad spct.idx
#>       <dbl>     <dbl> <fct>   
#>  1     290          0 time.04 
#>  2     290.         0 time.04 
#>  3     291.         0 time.04 
#>  4     291.         0 time.04 
#>  5     292.         0 time.04 
#>  6     292.         0 time.04 
#>  7     293.         0 time.04 
#>  8     293.         0 time.04 
#>  9     294.         0 time.04 
#> 10     294.         0 time.04 
#> # ℹ 3,176 more rows
set.seed(12345678)
pull_sample(sun_evening.mspct, 2)
#> Object: source_mspct [2 x 1]
#> --- Member: time.04 ---
#> Object: source_spct [1,593 x 2]
#> Wavelength range 290-1000 nm, step 0.34-0.47 nm 
#> Label: cosine.hour.9 
#> Measured on 2023-06-12 18:41:00.768459 UTC 
#> Measured at 60.227 N, 24.018 E; Viikki, Helsinki, FI 
#> Variables:
#>  w.length: Wavelength [nm]
#>  s.e.irrad: Spectral energy irradiance [W m-2 nm-1] 
#> --
#> # A tibble: 1,593 × 2
#>    w.length s.e.irrad
#>       <dbl>     <dbl>
#>  1     290          0
#>  2     290.         0
#>  3     291.         0
#>  4     291.         0
#>  5     292.         0
#>  6     292.         0
#>  7     293.         0
#>  8     293.         0
#>  9     294.         0
#> 10     294.         0
#> # ℹ 1,583 more rows
#> --- Member: time.05 ---
#> Object: source_spct [1,593 x 2]
#> Wavelength range 290-1000 nm, step 0.34-0.47 nm 
#> Label: cosine.hour.9 
#> Measured on 2023-06-12 18:42:00.769065 UTC 
#> Measured at 60.227 N, 24.018 E; Viikki, Helsinki, FI 
#> Variables:
#>  w.length: Wavelength [nm]
#>  s.e.irrad: Spectral energy irradiance [W m-2 nm-1] 
#> --
#> # A tibble: 1,593 × 2
#>    w.length s.e.irrad
#>       <dbl>     <dbl>
#>  1     290          0
#>  2     290.         0
#>  3     291.         0
#>  4     291.         0
#>  5     292.         0
#>  6     292.         0
#>  7     293.         0
#>  8     293.         0
#>  9     294.         0
#> 10     294.         0
#> # ℹ 1,583 more rows
#> 
#> --- END ---