Learn R Programming

hyperSpec (version 0.95)

Extract and Replace: Extract and Replace Methods for hyperSpec

Description

These Methods allow to extract and replace parts of the hyperSpec object. They work with respect to the spectra (rows of x), the columns of the data matrix, and the wavelengths (columns of the spectra matrix).

Thus, they can be used for selecting/deleting spectra, cutting the spectral range, and extracting or setting the data belonging to the spectra.

Convenient shortcuts for access of the spectra matrix and the data.frame in slot data are provided.

Usage

## S3 method for class 'hyperSpec':
[(x, i, j, l, \dots, wl.index = FALSE, short = NULL, drop = FALSE)

## S3 method for class 'hyperSpec': [[(x, i, j, l, \dots, wl.index = FALSE, drop = FALSE)

## S3 method for class 'hyperSpec': $(x, name)

Arguments

x
a hyperSpec Object
i
row index: selects spectra
j, name
selecting columns of x@data
l
selecting columns of the spectra matrix. If l is numeric, the default behaviour is treating l as wavelengths, not as indices.
wl.index
If TRUE (default), the value(s) in l are treated as column indices for the spectral matrix. Otherwise, the numbers in l are treated as wavelengths and the corresponding column indices are looked up first
short
short description for logentry.
drop
For [[: drop unnecessary dimensions, see drop and Extract. Ignored for [, as otherwise invalid hyperSpec
...
ignored

Value

  • For [, [<-, [[<-, and $<- a hyperSpec object, for [[ a matrix or data.frame, and for $ the column of the data.frame @data.

    x[[]] returns the complete spectra matrix.

    x$. returns the complete slot @data,

    x$.. returns the data.frame in @data but without the column @data$spc containing the spectra matrix.

Details

Extracting: [, [[, and $. The version with single square brackets ([) returns the resulting hyperSpec object.

[[ yields data.frame of slot @data of that corresponding hyperSpec object returned with the same arguments by [ if columns were selected (i.e. j is given), otherwise the spectra matrix x@data$spc. $ returns the selected column of the data.frame in slot @data. Shortcuts. Three shortcuts to conveniently extract much needed parts of the object are defined:

x[[]] returns the spectra matrix.

x$. returns the complete slot @data, including the spectra matrix in column $spc, as a data.frame.

x$.. returns a data.frame like x$. but without the spectra matrix.

Replacing: [<-, [[<-, and $<-. ## S4 method for signature 'hyperSpec': x[i, j, l, short = NULL, \dots] <- value

## S4 method for signature 'hyperSpec': x [[i, j, l, wl.index = FALSE, short = NULL, \dots]] <- value

## S4 method for signature 'hyperSpec': x$name <- value value gives the values to be assigned. For $, this con also be a list of the form list (value = value, label = label), with label containing the label for data column name.

[[<- replaces parts of the spectra matrix.

[<- replaces parts of the data.frame in slot x@data.

$<- replaces a column of the data.frame in slot x@data. The value may be a list with two elements, value and label. In this case the label of the data column is changed accordingly.

See Also

wl2i on conversion of wavelength ranges to indices. drop and Extract on drop.

Examples

Run this code
## index into the rows (spectra) -------------------------------------
## make some "spectra"

## numeric index
plot (flu, "spc", lines.args = list (lty = 2))
plot (flu[1:3], "spc", add = TRUE, col = "red")     # select spectra
plot (flu[-(1:3)], "spc", add = TRUE, col = "blue") # delete spectra

## logic index
plot (flu, "spc", lines.args = list (lty = 2))
index <- rnorm (6) > 0
index
plot (flu[index], "spc", add = TRUE, col = "red")   # select spectra
plot (flu[!index], "spc", add = TRUE, col = "blue") # select spectra

## index into the data columns ---------------------------------------
range (chondro[[,"x"]])
colnames (chondro[[,1]])
dim (chondro[[,c(TRUE, FALSE, FALSE)]])
chondro$x


## the shortcut functions --------------------------------------------

## extract the spectra matrix
flu[[]]
pca <- prcomp (flu[[]])

## extract the data.frame including the spectra matrix
flu$.
dim(flu$.)
colnames (flu$.)
flu$.$spc

calibration <- lm (spc ~ c, data = flu[,,450]$.)
calibration

flu$..
colnames (flu$..)

## replacement functions
spc <- flu
spc$.
spc[, "c"] <- 16 : 11
spc$.
spc$z <- 1 : 6
spc
spc$z <- list (1 : 6, "z / a.u.") 

## be careful:
plot (spc)
spc[] <- 6 : 1
spc$..
plot (spc)

spc[[]]
spc[[3]] <- -spc[[3]]
spc[[]]
spc[[,,405 : 410]] <- -spc[[,,405 : 410]]
spc[[]]

Run the code above in your browser using DataLab