Skip to contents

Copy metadata attributes from members of a generic_mspct object into a data.frame or a tibble.

Usage

add_attr2tb(
  tb = NULL,
  mspct,
  col.names = NULL,
  idx = "spct.idx",
  unnest = FALSE
)

when_measured2tb(mspct, tb = NULL, col.names = "when.measured", idx = NULL)

geocode2tb(mspct, tb = NULL, col.names = "geocode", idx = "spct.idx")

lonlat2tb(mspct, tb = NULL, col.names = c("lon", "lat"), idx = "spct.idx")

lon2tb(mspct, tb = NULL, col.names = "lon", idx = "spct.idx")

lat2tb(mspct, tb = NULL, col.names = "lat", idx = "spct.idx")

address2tb(mspct, tb = NULL, col.names = "address", idx = "spct.idx")

what_measured2tb(
  mspct,
  tb = NULL,
  col.names = "what.measured",
  idx = "spct.idx"
)

how_measured2tb(mspct, tb = NULL, col.names = "how.measured", idx = "spct.idx")

normalized2tb(mspct, tb = NULL, col.names = "normalized", idx = "spct.idx")

scaled2tb(mspct, tb = NULL, col.names = "scaled", idx = "spct.idx")

instr_desc2tb(
  mspct,
  tb = NULL,
  col.names = "instr.desc",
  fields = TRUE,
  idx = "spct.idx"
)

instr_settings2tb(
  mspct,
  tb = NULL,
  col.names = "instr.settings",
  fields = TRUE,
  idx = "spct.idx"
)

BSWF_used2tb(mspct, tb = NULL, col.names = "bswf.used", idx = "spct.idx")

filter_properties2tb(
  mspct,
  tb = NULL,
  col.names = "filter.properties",
  idx = "spct.idx"
)

solute_properties2tb(
  mspct,
  tb = NULL,
  col.names = "solute.properties",
  idx = "spct.idx"
)

Tfr_type2tb(mspct, tb = NULL, col.names = "Tfr.type", idx = "spct.idx")

Rfr_type2tb(mspct, tb = NULL, col.names = "Rfr.type", idx = "spct.idx")

time_unit2tb(mspct, tb = NULL, col.names = "time.unit", idx = "spct.idx")

comment2tb(mspct, tb = NULL, col.names = "comment", idx = "spct.idx")

multiple_wl2tb(mspct, tb = NULL, col.names = "multiple.wl", idx = "spct.idx")

Arguments

tb

tibble or data.frame to which to add the data (optional).

mspct

generic_mspct or generic_spct Any collection of spectra or one or more spectra in long form.

col.names

named character vector Name(s) of metadata attributes to copy. If named, the names provide the name for the columns.

idx

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

unnest

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

fields

character vector or logical Names of fields to extract from each descriptor record.

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

# Add attributes to irradiance
## from collection of spectra
e_irrad(sun_evening.mspct) |>
  add_attr2tb(sun_evening.mspct,
              c(when.measured = "time"))
#> # A tibble: 5 × 3
#>   spct.idx E_Total time               
#>   <fct>      <dbl> <dttm>             
#> 1 time.01     26.0 2023-06-12 18:38:00
#> 2 time.02     24.9 2023-06-12 18:39:00
#> 3 time.03     25.5 2023-06-12 18:40:00
#> 4 time.04     25.9 2023-06-12 18:41:00
#> 5 time.05     28.5 2023-06-12 18:42:00

## from spectra in long form
e_irrad(sun_evening.spct) |>
  add_attr2tb(sun_evening.spct,
              c(when.measured = "time"))
#> # A tibble: 5 × 4
#>   E_Total spct.idx when.measured       time               
#>     <dbl> <chr>    <dttm>              <dttm>             
#> 1    26.0 time.01  2023-06-12 18:38:00 2023-06-12 18:38:00
#> 2    24.9 time.02  2023-06-12 18:39:00 2023-06-12 18:39:00
#> 3    25.5 time.03  2023-06-12 18:40:00 2023-06-12 18:40:00
#> 4    25.9 time.04  2023-06-12 18:41:00 2023-06-12 18:41:00
#> 5    28.5 time.05  2023-06-12 18:42:00 2023-06-12 18:42:00

# Add attributes to transmittance
## from collection of spectra
transmittance(two_filters.mspct) |>
  add_attr2tb(two_filters.mspct, col.names = "what.measured")
#> # A tibble: 2 × 3
#>   spct.idx `Tfr(wl)_Total` what.measured                                        
#>   <fct>              <dbl> <chr>                                                
#> 1 spct_1             0.775 Polyester, clear film, 0.000125 m thick, Autostat CT…
#> 2 spct_2             0.426 Theatrical 'gel', type 'Canary Supergel no312; new; …

transmittance(two_filters.mspct) |>
  add_attr2tb(two_filters.mspct,
              col.names = c("filter.properties", "what.measured"),
              unnest = TRUE)
#> # A tibble: 2 × 6
#>   spct.idx `Tfr(wl)_Total` Rfr.constant thickness attenuation.mode what.measured
#>   <fct>              <dbl>        <dbl>     <dbl> <chr>            <chr>        
#> 1 spct_1             0.775        0.074  0.000125 absorption       Polyester, c…
#> 2 spct_2             0.426        0.08   0.00009  absorption       Theatrical '…

# Create a new data frame
add_attr2tb(mspct = two_filters.mspct,
            idx = "filter",
            col.names = c("filter.properties", "what.measured"),
            unnest = TRUE)
#> # A tibble: 2 × 5
#>   filter Rfr.constant thickness attenuation.mode what.measured                  
#>   <fct>         <dbl>     <dbl> <chr>            <chr>                          
#> 1 spct_1        0.074  0.000125 absorption       Polyester, clear film, 0.00012…
#> 2 spct_2        0.08   0.00009  absorption       Theatrical 'gel', type 'Canary…