Extract and Replace {hyperSpec} | R Documentation |
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.
## S4 method for signature 'hyperSpec': x[i, j, l, ..., wl.index = FALSE, short = NULL, drop = FALSE] ## S4 method for signature 'hyperSpec': x[[i, j, l, ..., wl.index = FALSE, drop = FALSE]] ## S4 method for signature 'hyperSpec': x$name
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 via wl2i . |
short |
short description for logentry . |
drop |
For [[ : drop unnecessary dimensions, see
drop and Extract . Ignored for
[ , as otherwise invalid hyperSpec objects might result. |
... |
ignored |
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, ...] <- value ## S4 method for signature 'hyperSpec': x [[i, j, l, wl.index = FALSE, short = NULL, ...]] <- value ## S4 method for signature 'hyperSpec': x$name <- value
value
gives the values to be assigned.$
, 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.
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.
wl2i
on conversion of wavelength ranges to
indices.
## 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[[]]