=
instead of arrow <-
_
, unless you’re overloading an R generic e.g. as.matrix
.i_
prefix. e.g i_find_spectra()
spectra
object’s guts. If you’re accessing the internal data structuresdirectly, you’re probably doing something wrong.spectra
object, we did something wrong. Please report an issue and submit a pull request.spectra
object, even if doing so though the getters and setters.library("spectrolab")
## spectrolab version: 0.0.4
##
## Please cite:
## Meireles J, Schweiger A and Cavender-Bares J (2017). spectrolab: Class
## and Methods for Hyperspectral Data. R package version 0.0.4, <URL:
## https://github.com/meireles/spectrolab>.
##
## Attaching package: 'spectrolab'
## The following objects are masked from 'package:stats':
##
## sd, smooth, var
spectrolab
defines a new S3 class called spectra
that holds all of the different compnents of a spectral data.
Without diving too much into its implementation, a spectra
object holds the important information needed for most spectral datasets: reflectance, wavelengths, etc. The class has a bunch of requirements in terms of both format and values.
Some extra requirements can be added For example, reflectances can by default take any numeric value, but you can pass enforce01 = TRUE
to the spectra()
constructor to ensure that all valudes will be between 0 and 1.
spectra
object “by hand”In addition to read_spectra()
and as.spectra()
, you can create a spectra
object “by hand”" using the more flexible spectra()
constructor, which takes at least arguments: (1) a reflectance matrix, (2) a vector of wavelengths and (3) the sample names.
# (1) Create a reflectance matrix.
# In this case, by removing the first column that holds the species name
rf = spec_matrix_example[ , -1]
# (2) Create a vector with wavelength labels that match
# the reflectance matrix columns.
wl = colnames(rf)
# (3) Create a vector with sample labels that match
# the reflectance matrix rows.
# In this case, use the first colum of spec_matrix_example
sn = spec_matrix_example[ , 1]
# Finally, construct the spectra object using the `spectra` constructor
spec = spectra(reflectance = rf, wavelengths = wl, names = sn)
# And hopefully this worked fine
is_spectra(spec)
## [1] TRUE
plot(spec)
spectrolab
gives you acess to get and set functions for most spectra
components. The names()
, wavelengths()
functions do both getting and setting. For example:
# Getters
names(spec)[1:4]
wavelengths(spec)[1:4]
# Setters
names(spec) = toupper(names(spec))
wavelengths(spec) = wavelengths(spec) / 1000
Reflectances are set using the [
notation. For instance:
spec[1, 400:1200] = spec[1, 400:1200] * 2
plot(spec)