library(hyperSpec) library(hySpc.dplyr) library(hySpc.chondro) knitr::opts_chunk$set( collapse = TRUE, comment = "#>", results = "hide" )
This vignette introduces package hyperSpec.dplyr
.
hySpc.dplyr
"fortifies" hyperSpec
objects so that they can be used in tidyverse
style.
In particular, this piping becomes convenient.
On a more technical level, hyperSpec.dplyr
provides functions such as filter
, select
, and so on for hyperSpec
objects.
The spectra matrix in $spc
behaves a bit different from the extra-data columns, because it contains a whole matrix inside one column.
Many tidyverse
functions can directly deal with this.
In other cases, special attention is needed -
this is explained in this vignette.
filter()
or slice()
dplyr::filter() selects rows, and filter
ing by extra data columns works as with any data.frame
:
flu %>% filter (c > 0.2) flu %>% filter (c %>% between (0.15, 0.25))
The filter conditions (logical predicates) must yield one logical value per row.
Logical expressions on the spectra matrix such as spc > 100
yield a logical matrix.
This matrix indicates which elements of the spectra matrix match the condition.
To obtain the logical vector that indicates which spectra (rows) should be kept, further information is needed:
should a spectrum be kept if any of its intensity values (spectra matrix elements) fulfills the condition,
or only if all of them do?
hyperSpec
functions any_wl()
and all_wl()
summarize the logical matrix in this way.
Typical filtering tasks based on the spectra matrix are:
NA
s at all wavelengths (which may be produced by certain import functions):
r
flu %>%
filter (!all_wl (is.na (spc)))
NA
s at any wavelength (for example after spikes or other artifacts have been marked by NA
s):
r
flu %>%
filter (!any_wl (is.na (spc)))
r
flu %>%
filter (all_wl (between (spc, 0, 500)))
r
flu %>%
filter (rowMeans (spc) %>% between (0, 500))
Here, any_wl()
or all_wl()
are not needed: the spectra are already summarized into a single intensity value by rowMeans()
.
The condition therefore evaluates to a one logical value per spectrum already - which is what is needed.To select rows by indices rather than a condition, use slice():
chondro %>% slice (1:3) chondro %>% slice (800 : n()) chondro %>% slice (-10 : -n()) flu %>% slice (1, 3, 5)
Also, head()
and tail()
work as usual.
select()
select()
selects or discards particular columns from a hyperSpec
object.
If the spectra matrix (colum spc
) is included in the selection, the result is still a hyperSpec
object:
chondro %>% select (clusters, spc)
If the result does not have the spectra matrix in $spc
, it will be returned as data.frame
:
flu %>% select (-spc)
To convert such a data.frame
into a hyperSpec
object again, use as.hyperSpec
:
flu %>% select (-spc) %>% as.hyperSpec ()
The resulting hyperSpec
object has 0 wavelengths: its $spc
column contains a spectra matrix with 0 columns, and its wavelength vector is also of length 0.
Behind the scenes, the data.frame
returned by select()
gets an attribute labels
which stores the labels of the hyperSpec
object.
as.hyperSpec()
restores the labels from this attribute (if available).
Therfore, the "back-converted" hyperSpec
object has its labels preserved.
rename()
rename()
renames extra data columns from the hyperSpec
object. Unlike select()
rename()
keeps all the variables of the data frame intact.
chondro %>% rename(region = clusters)
mutate()
mutate()
adds new variables and preserves all the existing variables.
laser %>% mutate (t, filename) head # => results in a hyperSpec object laser %>% mutate (-spc) # => results in a hyperSpec object laser %>% mutate (spc2 = spc*2) %>% mutate (spc2) %>% mutate (spc2*2) # => results in a hyperSpec object
transmute()
transmute()
mutates the data columns of hyperspec
objects. Unlike mutate()
transmute()
adds new variables but drops existing ones.
laser %>% transmute (t, filename) head # => results in a data frame laser %>% transmute (-spc) # => results in a hyperSpec object laser %>% transmute (spc2 = spc*2) %>% transmute (spc2) %>% transmute (spc2*2) # => results in a hyperSpec object
setLabels()
setLabels()
Ensure that hyperSpec and non hyperSpec objects have the correct labels.
flu %>% setLabels(.wavelength = "f / THz", c = "c / ml")
TODO
sessionInfo()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.