Skip to contents

Return metadata attributes from a single spectrum or a collection of spectra as a data.frame. A wrapper on add_attr2tb providing an alternative order of formal parameters and constrained functionality.

Usage

spct_metadata(
  x,
  col.names = NULL,
  idx = "spct.idx",
  na.rm = is.null(col.names),
  unnest = TRUE
)

Arguments

x

generic_mspct or generic_spct Any collection of spectra or spectrum.

col.names

named character vector Name(s) of column(s) to create.

idx

character Name of the column with the names of the members of the collection of spectra.

na.rm

logical Flag controlling deletion of columns containing only NA values.

unnest

logical Flag controlling if metadata attributes that are lists of values should be returned in a list column or in separate columns.

Value

A data.frame or a tibble With the metadata attributes in separate new variables.

Details

Each attribute is by default copied to a column in a tibble or a data.frame. If the argument for tb is NULL, as by default, a new tibble will be created. If an existing data.frame or tibble is passed as argument, new columns are added to it. However, the number of rows in the argument passed to tb must match the number of spectra in the argument passed to mspct. Only in the case of methods add_attr2tb() and spct_metadata() if the argument to col.names is a named vector, the names of members are used as names for the columns created. This permits setting any valid name for the new columns. If the members of the vector passed to col.names have no names, then the value is interpreted as the name of the attributes to add, and also used as name for the new column.

Valid values accepted as argument to col.names are NULL, or a vector containing one or more of the following character strings: "lon", "lat", "address", "geocode", "where.measured", "when.measured", "what.measured", "how.measured", "comment", "normalised", "normalized", "scaled", "bswf.used", "instr.desc", "instr.sn", solute.properties, "filter.properties", "Tfr.type", "Rfr.type", "time.unit", bswf.used, multiple.wl. Invalid character values are ignored with a warning.

Note

The order of the first two arguments is reversed in add_attr2tb(), when_measured2tb(), what_measured2tb(), etc., compared to attribute query functions, such as spct_metadata, when_measured(), what_measured(), how_measured(), etc. This is to allow the use of add_attr2tb() and related functions in 'pipes' to add metadata to summaries computed at earlier steps in the pipe.

Examples

# collection of spectra
spct_metadata(sun_evening.mspct)
#> # A tibble: 5 × 11
#>   spct.idx   lat   lon address    when.measured       what.measured how.measured
#>   <fct>    <dbl> <dbl> <chr>      <dttm>              <chr>         <chr>       
#> 1 time.01   60.2  24.0 Viikki, H… 2023-06-12 18:38:00 cosine.hour.9 "Acquired w…
#> 2 time.02   60.2  24.0 Viikki, H… 2023-06-12 18:39:00 cosine.hour.9 "Acquired w…
#> 3 time.03   60.2  24.0 Viikki, H… 2023-06-12 18:40:00 cosine.hour.9 "Acquired w…
#> 4 time.04   60.2  24.0 Viikki, H… 2023-06-12 18:41:00 cosine.hour.9 "Acquired w…
#> 5 time.05   60.2  24.0 Viikki, H… 2023-06-12 18:42:00 cosine.hour.9 "Acquired w…
#> # ℹ 4 more variables: normalized <dbl>, multiplier <dbl>, time.unit <Duration>,
#> #   bswf.used <chr>

spct_metadata(sun_evening.mspct, na.rm = FALSE)
#> # A tibble: 5 × 17
#>   spct.idx   lat   lon address    when.measured       what.measured how.measured
#>   <fct>    <dbl> <dbl> <chr>      <dttm>              <chr>         <chr>       
#> 1 time.01   60.2  24.0 Viikki, H… 2023-06-12 18:38:00 cosine.hour.9 "Acquired w…
#> 2 time.02   60.2  24.0 Viikki, H… 2023-06-12 18:39:00 cosine.hour.9 "Acquired w…
#> 3 time.03   60.2  24.0 Viikki, H… 2023-06-12 18:40:00 cosine.hour.9 "Acquired w…
#> 4 time.04   60.2  24.0 Viikki, H… 2023-06-12 18:41:00 cosine.hour.9 "Acquired w…
#> 5 time.05   60.2  24.0 Viikki, H… 2023-06-12 18:42:00 cosine.hour.9 "Acquired w…
#> # ℹ 10 more variables: normalized <dbl>, multiplier <dbl>, f <lgl>,
#> #   range <list>, target <dbl>, cols <chr>, time.unit <Duration>,
#> #   bswf.used <chr>, Tfr.type <chr>, Rfr.type <chr>

spct_metadata(sun_evening.mspct,
              col.names = "geocode",
              unnest = FALSE)
#> # A tibble: 5 × 2
#>   spct.idx geocode         
#>   <fct>    <named list>    
#> 1 time.01  <tibble [1 × 3]>
#> 2 time.02  <tibble [1 × 3]>
#> 3 time.03  <tibble [1 × 3]>
#> 4 time.04  <tibble [1 × 3]>
#> 5 time.05  <tibble [1 × 3]>

spct_metadata(sun_evening.mspct,
              col.names = c(when.measured = "time", "what.measured"))
#> # A tibble: 5 × 3
#>   spct.idx time                what.measured
#>   <fct>    <dttm>              <chr>        
#> 1 time.01  2023-06-12 18:38:00 cosine.hour.9
#> 2 time.02  2023-06-12 18:39:00 cosine.hour.9
#> 3 time.03  2023-06-12 18:40:00 cosine.hour.9
#> 4 time.04  2023-06-12 18:41:00 cosine.hour.9
#> 5 time.05  2023-06-12 18:42:00 cosine.hour.9

# multiple spectra in long form
spct_metadata(sun_evening.spct,
              col.names = c("geocode", "when.measured"))
#> # A tibble: 5 × 5
#>   spct.idx   lat   lon address              when.measured      
#>   <fct>    <dbl> <dbl> <chr>                <dttm>             
#> 1 time.01   60.2  24.0 Viikki, Helsinki, FI 2023-06-12 18:38:00
#> 2 time.02   60.2  24.0 Viikki, Helsinki, FI 2023-06-12 18:39:00
#> 3 time.03   60.2  24.0 Viikki, Helsinki, FI 2023-06-12 18:40:00
#> 4 time.04   60.2  24.0 Viikki, Helsinki, FI 2023-06-12 18:41:00
#> 5 time.05   60.2  24.0 Viikki, Helsinki, FI 2023-06-12 18:42:00

# single spectrum
spct_metadata(sun.spct,
              col.names = c("geocode", "when.measured"))
#> # A tibble: 1 × 5
#>   spct.idx    lat   lon address               when.measured      
#>   <fct>     <dbl> <dbl> <chr>                 <dttm>             
#> 1 spct.name  60.2  25.0 Kumpula, Helsinki, FI 2010-06-22 09:51:00