extractreplace: Extract and Replace parts of hyperSpec objects

[,hyperSpec-methodR Documentation

Extract and Replace parts of hyperSpec objects

Description

These Methods allow to extract and replace parts of the hyperSpec object.

Usage

## S4 method for signature 'hyperSpec'
x[i, j, l, ..., wl.index = FALSE, drop = FALSE]

## S4 method for signature 'hyperSpec'
x[[i, j, l, ..., wl.index = FALSE, drop = FALSE]]

## S4 method for signature 'hyperSpec'
x$name

## S4 replacement method for signature 'hyperSpec'
x[i, j, ...] <- value

## S4 replacement method for signature 'hyperSpec'
x[[i, j, l, wl.index = FALSE, ...]] <- value

## S4 replacement method for signature 'hyperSpec'
x$name <- value

Arguments

x

a hyperSpec Object

i

row index: selects spectra

[[ and [[<- accept indexing with logical matrix or a n by 2 integer index matrix. In this case the indexing is done inside the spectra matrix. See the examples below.

j

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.

...

ignored

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 via wl2i.

drop

For [[: drop unnecessary dimensions, see drop and Extract. Ignored for [, as otherwise invalid hyperSpec objects might result.

name

name of the data column to extract. $spc yields the spectra matrix.

value

the replacement value

Details

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.

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, \dots] <- value

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

## S4 method for signature 'hyperSpec':
x$name <- value

value gives the values to be assigned.

For $, this can 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.

$..<- is again an abbreviation for the data.frame without the spectra matrix.

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.

See Also

wl2i on conversion of wavelength ranges to indices.

drop and Extract on drop.

Examples


## 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[[]]

## indexing via logical matrix
summary (flu [[flu < 125]])

## indexing the spectra matrix with index matrix n by 2
ind <- matrix (c (1, 2, 4, 406, 405.5, 409), ncol = 2)
ind
flu [[ind]]

ind <- matrix (c (1, 2, 4, 4:6), ncol = 2)
ind
flu [[ind, wl.index = TRUE]]

pca <- prcomp (flu[[]])

## result is data.frame, if j is given:
result <- flu [[, 1:2, 405 ~ 410]]
result
class (result)
colnames (result)

## 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
## be careful:
plot (spc)
spc [] <- 6 : 1
spc$..
plot (spc)

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

## indexing with logical matrix
spc <- flu [,, min ~ 410]
spc < 125
spc [[spc < 125]] <- NA
spc [[]]

## indexing with n by 2 matrix
ind <- matrix (c (1, 2, 4, 406, 405.5, 409), ncol = 2)
ind
spc [[ind]] <- 3
spc [[]]

ind <- matrix (c (1, 2, 4, 4:6), ncol = 2)
ind
spc [[ind, wl.index = TRUE]] <- 9999
spc [[]]

spc$.
spc$z <- 1 : 6
spc
spc$z <- list (1 : 6, "z / a.u.")


hyperSpec documentation built on May 29, 2024, 7:28 a.m.