Learn R Programming

spectacles (version 0.5-4)

spectra-methods: Retrieves or sets the spectra of a Spectra* objects.

Description

Either retrieves the spectra matrix from a Spectra* object, or creates a Spectra* object from a "data.frame" object different interfaces detailed below.When applied to a Spectra* object, this functions simply returns the spectra it is storing.

Usage

# S4 method for Spectra
spectra(object)

# S4 method for data.frame spectra(object, ...) <- value

# S4 method for Spectra spectra(object) <- value

Arguments

object

an object of class "Spectra" or inheriting from this class

...

see details below

value

see details below

Author

Pierre Roudier pierre.roudier@gmail.com

Details

If applied on a "data.frame" object, it is an helper function to create a Spectra* object. Two kind of interfaces are then available. value can be:

a vector:

Similarly to wl, the wavelengths of the spectra can be passed by a "numeric" vector. Alternatively, the names of the columns that contain the spectra information can be passed as a "character" vector.

a formula:

This interface is specific to inspectr. It follows a scheme where differents parts can be observed, the id column, the attributes columns, and the spectra columns, described by the wavelengths at which it has been measured:

Placeholders:

  • ... placeholder for all the columns of your data.frame object except those that have been already used in other parts of the formula. This can lead to errors. E.g. if object has data one every wavelength between 350 and 2500 nm, spectra(object) <- id_field ~ ... ~ 500:2500 will stores the columns corresponding to the wavelengths 350-499 nm in its data slot!

  • id For the creation of a SpectraDataFrame, it is important to always specify an id field in the formula. If no id column is present, the id placeholder will create one for you.

spectra(object) <- ~ 350:2500 will build a Spectra object from the wavelengths between 350 and 2500, based on the column names. spectra(object) <- ~ 350:2:2500 will build a Spectra object from the wavelengths in seq(350, 2500, by = 2) spectra(object) <- ~ 500:2350 will build a Spectra object from the wavelengths between 500 and 2350, even though other wavelengths are present (they will be dropped) In the three later cases, the id field has been dropped (it will be automatically created). If you want to use a column of "data.frame" as an id filed, you can still use the first part of the formula: spectra(object) <- id_field ~ 350:2500 spectra(object) <- id_field ~ 350:5:2500 Some data can also be added to the object, which will then be of SpectraDataFrame class: spectra(object) <- id_field ~ property1 ~ 500:2300 will create a SpectraDataFrame with ids from the id_field column, data from the property1 column, and spectral information between 500 and 2300 nm. That means that data property2, and all spectral information from bands < 500 nm or > 2300 nm will be dropped You can also combine the placeholders:

spectra(object) <- id_field ~ ... ~ 350:2500 will create a SpectraDataFrame object with ids from the id_field column, all spectral bands between 350 and 2500 nm. The data slot is given all the remaining columns.

Examples

Run this code

# Loading example data
data(oz)
class(oz) # this is a simple data.frame
# structure of the data.frame: it is rowwise-formatted
big.head(oz) 

## CREATING Spectra OBJECTS
##

# Using spectra() to initiate a Spectra from 
# the data.frame
spectra(oz) <- sr_no ~ 350:2500

# It is possible to select wavelengths using the formula interface
data(oz)
spectra(oz) <- sr_no ~ 350:5:2500

data(oz)
spectra(oz) <- sr_no ~ 500:1800

## CREATING SpectraDataFrame OBJECTS
##

# Using spectra() to initiate a SpectraDataFrame from 
# the data.frame
data(oz)
spectra(oz) <- sr_no ~ carbon + ph + clay ~ 350:2500

# Selecting data to be included in the SpectradataFrame object
data(oz)
spectra(oz) <- sr_no ~ carbon ~ 350:2500

# Forcing the creation of new ids using the id keyword in the 
# formula interface
data(oz)
spectra(oz) <- id ~ carbon ~ 350:2500
ids(oz, as.vector = TRUE)

# Using the "..." short-hand to select all the remaining columns
data(oz)
spectra(oz) <- sr_no ~ ... ~ 350:2500

## CREATING Spectra OBJECTS FROM
## BY-COLS-FORMATTED DATA
##

# For data formatted in the colwise format, 
# use the "colwise" mode

# Transforming data into colwise format
# for demonstration's sake
#
m <- melt_spectra(oz)
oz_by_col <- reshape2::acast(m, ... ~ sr_no)
oz_by_col <- data.frame(
  wl = rownames(oz_by_col), 
  oz_by_col, 
  check.names = FALSE)

# Here's colwise-formatted data 
big.head(oz_by_col)

# Convert it into Spectra object
spectra(oz_by_col, mode = "colwise") <- wl ~ ...

# Then data can be added to promote it as a SpectraDataFrame
my.data <- features(oz)
features(oz_by_col) <- my.data

Run the code above in your browser using DataLab