Title: | Functions to Handle and Preprocess Infrared Spectra |
Version: | 0.4.1 |
Description: | Functions to import and handle infrared spectra (import from '.csv' and Thermo Galactic's '.spc', baseline correction, binning, clipping, interpolating, smoothing, averaging, adding, subtracting, dividing, multiplying, atmospheric correction, 'tidyverse' methods, plotting). |
License: | GPL-3 |
Encoding: | UTF-8 |
LazyData: | true |
RoxygenNote: | 7.3.2 |
Depends: | R (≥ 4.1.0) |
Imports: | tidyr, dplyr, purrr, tibble, ggplot2, stringr, hyperSpec (≥ 0.99.20200527), grDevices, rlang, methods, units, Rdpack, magrittr, stats, lifecycle |
Suggests: | baseline, ChemoSpec (≥ 5.2.12), kableExtra, fda, knitr, quantities, rmarkdown, signal, spelling, vctrs, tidyselect, prospectr |
VignetteBuilder: | knitr |
RdMacros: | Rdpack |
Date: | 2025-04-06 |
URL: | https://henningte.github.io/ir/, https://github.com/henningte/ir/ |
BugReports: | https://github.com/henningte/ir/issues/ |
Language: | en-US |
NeedsCompilation: | no |
Packaged: | 2025-04-06 04:17:46 UTC; henni |
Author: | Henning Teickner |
Maintainer: | Henning Teickner <henning.teickner@uni-muenster.de> |
Repository: | CRAN |
Date/Publication: | 2025-04-06 04:30:02 UTC |
ir: Functions to Handle and Preprocess Infrared Spectra
Description
Functions to import and handle infrared spectra (import from '.csv' and Thermo Galactic's '.spc', baseline correction, binning, clipping, interpolating, smoothing, averaging, adding, subtracting, dividing, multiplying, atmospheric correction, 'tidyverse' methods, plotting).
Author(s)
Maintainer: Henning Teickner henning.teickner@uni-muenster.de (ORCID) [copyright holder]
See Also
Useful links:
Report bugs at https://github.com/henningte/ir/issues/
Pipe operator
Description
See magrittr::[\%>\%][magrittr::pipe]
for details.
Usage
lhs %>% rhs
Arguments
lhs |
A value or the magrittr placeholder. |
rhs |
A function call using the magrittr semantics. |
Value
The result of calling rhs(lhs)
.
Arithmetic operations for ir
objects
Description
Arithmetic operations for ir
objects
Usage
## S3 method for class 'ir'
Ops(e1, e2)
Arguments
e1 |
An object of class |
e2 |
An object of class |
Value
e1
with intensity values of the spectra added to/subtracted
with/multiplied with/divided by those in e2
:
If
e2
is a numeric value, all intensity values in the spectra ofe1
are added/subtracted/multiplied/divided bye2
.If
e2
is anir
object with one row, it is replicated (see rep.ir) so that the row numbers match to those ofe1
and intensity values are added/subtracted/multiplied/divided row-wise.If
e2
is anir
object with the same number of rows ase1
, intensity values are added/subtracted/multiplied/divided row-wise.
Examples
## addition
ir::ir_sample_data + ir::ir_sample_data
ir::ir_sample_data + 2
ir::ir_sample_data +
seq(from = 0, to = 2, length.out = nrow(ir::ir_sample_data))
## subtraction
ir::ir_sample_data - ir::ir_sample_data
ir::ir_sample_data - 2
ir::ir_sample_data -
seq(from = 0, to = 2, length.out = nrow(ir::ir_sample_data))
## multiplication
ir::ir_sample_data * ir::ir_sample_data
ir::ir_sample_data * 2
ir::ir_sample_data *
seq(from = 0, to = 2, length.out = nrow(ir::ir_sample_data))
## division
ir::ir_sample_data / ir::ir_sample_data
ir::ir_sample_data / 2
ir::ir_sample_data /
seq(from = 0.1, to = 2, length.out = nrow(ir::ir_sample_data))
Arrange rows in ir
objects by column values
Description
Arrange rows in ir
objects by column values
Usage
arrange.ir(.data, ..., .by_group = FALSE)
Arguments
.data |
An object of class |
... |
< |
.by_group |
If |
Value
.data
with arranged rows.
Source
See Also
Other tidyverse:
distinct.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## arrange
dplyr::arrange(ir_sample_data, dplyr::desc(sample_type))
Bind rows of ir
objects
Description
Bind rows of ir
objects
Usage
## S3 method for class 'ir'
rbind(..., deparse.level = 1)
## S3 method for class 'ir'
cbind(..., deparse.level = 1)
Arguments
... |
Objects to bind together. For |
deparse.level |
An integer value; see |
Value
An object of class ir
. rbind
returns all input
ir
objects combined row-wise. cbind
returns the input ir
object and the other objects combined column-wise.
Examples
# rbind
rbind(ir_sample_data, ir_sample_data)
rbind(ir_sample_data |> dplyr::select(spectra),
ir_sample_data |> dplyr::select(spectra))
# cbind
cbind(ir_sample_data, a = seq_len(nrow(ir_sample_data)))
Subset distinct/unique rows in ir
objects
Description
Subset distinct/unique rows in ir
objects
Usage
distinct.ir(.data, ..., .keep_all = FALSE)
Arguments
.data |
An object of class |
... |
< |
.keep_all |
If |
Value
.data
with distinct rows.
Source
See Also
Other tidyverse:
arrange.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## distinct
dplyr::distinct(rep(ir_sample_data, 2))
Extract a character column in an ir
object into multiple columns using regular expression groups
Description
Extract a character column in an ir
object into multiple columns using regular expression groups
Usage
extract.ir(
data,
col,
into,
regex = "([[:alnum:]]+)",
remove = TRUE,
convert = FALSE,
...
)
Arguments
data |
An object of class |
col |
< |
into |
Names of new variables to create as character vector.
Use |
regex |
A string representing a regular expression used to extract the
desired values. There should be one group (defined by |
remove |
If |
convert |
If NB: this will cause string |
... |
Additional arguments passed on to methods. |
Value
data
with an extracted character column. See
tidyr::extract()
.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## extract
ir_sample_data |>
tidyr::extract(
id_sample, "a"
)
Filtering joins for an ir
object
Description
Filtering joins for an ir
object
Usage
semi_join.ir(x, y, by = NULL, copy = FALSE, ..., na_matches = c("na", "never"))
anti_join.ir(x, y, by = NULL, copy = FALSE, ..., na_matches = c("na", "never"))
Arguments
x |
An object of class |
y |
A data frame. |
by |
A join specification created with If To join on different variables between To join by multiple variables, use a
For simple equality joins, you can alternatively specify a character vector
of variable names to join by. For example, To perform a cross-join, generating all combinations of |
copy |
If |
... |
Other parameters passed onto methods. |
na_matches |
Should two |
Value
x
and y
joined. If the spectra
column is renamed, the ir
class is dropped. See filter-joins
.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter.ir()
,
group_by
,
mutate
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## semi_join
set.seed(234)
dplyr::semi_join(
ir_sample_data,
tibble::tibble(
id_measurement = c(1:5, 101:105),
nitrogen_content = rbeta(n = 10, 0.2, 0.1)
),
by = "id_measurement"
)
## anti_join
set.seed(234)
dplyr::anti_join(
ir_sample_data,
tibble::tibble(
id_measurement = c(1:5, 101:105),
nitrogen_content = rbeta(n = 10, 0.2, 0.1)
),
by = "id_measurement"
)
Subset rows in ir
objects using column values
Description
Subset rows in ir
objects using column values
Usage
filter.ir(.data, ..., .preserve = FALSE)
Arguments
.data |
An object of class |
... |
< |
.preserve |
Relevant when the |
Value
.data
with filtered rows.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter-joins
,
group_by
,
mutate
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## filter
dplyr::filter(ir_sample_data, sample_type == "office paper")
Group rows in ir
objects by one or more variables
Description
Group rows in ir
objects by one or more variables
Usage
group_by.ir(
.data,
...,
.add = FALSE,
.drop = dplyr::group_by_drop_default(.data)
)
ungroup.ir(.data, ...)
Arguments
.data |
An object of class |
... |
In |
.add |
When This argument was previously called |
.drop |
Drop groups formed by factor levels that don't appear in the
data? The default is |
Value
.data
with grouped rows (group_by.ir()
) or ungrouped rows
(ungroup.ir()
).
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
mutate
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## group_by
dplyr::group_by(ir_sample_data, sample_type)
## ungroup
dplyr::ungroup(dplyr::group_by(ir_sample_data, sample_type))
Add infrared spectra
Description
ir_add
takes two objects of class ir
, x
and
y
, and adds the intensity values of spectra in matching rows from
y
to that of x
.
Usage
ir_add(x, y)
Arguments
x |
An object of class |
y |
An object of class |
Value
x
where for each spectrum the respective intensity values in
y
are added.
Examples
x1 <-
ir::ir_add(ir::ir_sample_data, ir::ir_sample_data)
x2 <-
ir::ir_add(ir::ir_sample_data, ir::ir_sample_data[1, ])
# adding a numeric value to an object of class `ir`.
x3 <-
ir::ir_add(ir::ir_sample_data, 1)
# adding a numeric vector from an object of class `ir`.
x4 <-
ir::ir_add(
ir::ir_sample_data,
seq(from = 0, to = 2, length.out = nrow(ir::ir_sample_data))
)
Converts an object to class ir
Description
ir_as_ir
converts an object to an object of class ir
.
Usage
ir_as_ir(x, ...)
## S3 method for class 'ir'
ir_as_ir(x, ...)
## S3 method for class 'data.frame'
ir_as_ir(x, ...)
## S3 method for class 'ir_flat'
ir_as_ir(x, ...)
## S3 method for class 'hyperSpec'
ir_as_ir(x, ...)
## S3 method for class 'Spectra'
ir_as_ir(x, ...)
Arguments
x |
An object. |
... |
Further arguments passed to individual methods.
|
Value
An object of class ir
with available metadata from original
objects.
Examples
# conversion from an ir object
ir::ir_sample_data |>
ir_as_ir()
# conversion from a data frame
x_ir <- ir::ir_sample_data
x_df <-
x_ir |>
ir_drop_spectra() |>
dplyr::mutate(
spectra = x_ir$spectra
) |>
ir_as_ir()
# check that ir_as_ir preserves the input class
ir_sample_data |>
structure(class = setdiff(class(ir_sample_data), "ir")) |>
dplyr::group_by(sample_type) |>
ir_as_ir()
# conversion from an ir_flat object
x_ir <-
ir::ir_sample_data |>
ir::ir_flatten() |>
ir::ir_as_ir()
# conversion from a hyperSpec object from package hyperSpec
if(requireNamespace("hyperSpec")) {
x_hyperSpec <- hyperSpec::laser
x_ir <- ir_as_ir(x_hyperSpec)
}
# conversion from a Spectra object from class ChemoSpec
if(requireNamespace("ChemoSpec")) {
## sample data
x <- ir_sample_data
x_flat <- ir_flatten(x)
## creation of the object of class "Spectra" (the ChemoSpec package does
## not contain a sample Spectra object)
n <- nrow(x)
group_vector <- seq(from = 1, to = n, by = 1)
color_vector <- rep("black", times = n)
x_Spectra <- list() # dummy list
x_Spectra$freq <- as.numeric(x_flat[,1, drop = TRUE]) # wavenumber vector
x_Spectra$data <- as.matrix(t(x_flat[,-1])) # absorbance values as matrix
x_Spectra$names <- as.character(seq_len(nrow(x))) # sample names
x_Spectra$groups <- as.factor(group_vector) # grouping vector
x_Spectra$colors <- color_vector # colors used for groups in plots
x_Spectra$sym <- as.numeric(group_vector) # symbols used for groups in plots
x_Spectra$alt.sym <- letters[as.numeric(group_vector)] # letters used for groups in plots
x_Spectra$unit <- c("wavenumbers", "intensity") # unit of x and y axes
x_Spectra$desc <- "NULL" # optional descriptions in plots
attr(x_Spectra, "class") <- "Spectra"
# conversion to ir
x_ir <- ir_as_ir(x_Spectra)
}
Averages infrared spectra within groups
Description
ir_average
averages infrared spectra within a user-defined group. NA
values are omitted by default.
Usage
ir_average(x, ..., na.rm = TRUE, .groups = "drop")
Arguments
x |
An object of class |
... |
Variables in |
na.rm |
A logical value indicating if |
.groups |
When
In addition, a message informs you of that choice, unless the result is ungrouped,
the option "dplyr.summarise.inform" is set to |
Value
An object of class ir
where spectra have been averaged within
groups defined by ...
.
Examples
# average the sample data spectra across sample types
x <-
ir_sample_data |>
ir_average(sample_type)
Performs baseline correction on infrared spectra
Description
ir_bc
performs baseline correction for infrared spectra. Baseline
correction is either performed by using a polynomial with user defined
degree fitted to each spectrum (see ChemoSpec::baselineSpectra()
), or by
using a rubberband function that is fitted to each spectrum (see
hyperSpec::spc.rubberband()
), or using a Savitzky-Golay smoothed version of
the input spectra (see ir_bc_sg()
).
Usage
ir_bc(x, method = "rubberband", return_bl = FALSE, ...)
Arguments
x |
An object of class |
method |
A character value indicating which method should be used
for baseline correction. If |
return_bl |
A logical value indicating if for each spectrum the baseline
should be returned instead of the corrected intensity values
( |
... |
Further arguments passed to |
Value
An object of class ir
with the baseline corrected spectra, or if
return_bl = TRUE
, the baselines instead of the spectra in column spectra
.
Examples
library(dplyr)
# rubberband baseline correction
x1 <-
ir::ir_sample_data |>
dplyr::slice(1:10) |>
ir::ir_bc(method = "rubberband")
# polynomial baseline correction
if(!requireNamespace("ChemoSpec", quietly = TRUE)) {
x2 <-
ir::ir_sample_data |>
dplyr::slice(1:10) |>
ir::ir_bc(method = "polynomial", degree = 2)
}
# Savitzky-Golay baseline correction
if(!requireNamespace("signal", quietly = TRUE)) {
x3 <-
ir::ir_sample_data |>
dplyr::slice(1:10) |>
ir::ir_bc(method = "sg", p = 3, n = 199, ts = 1, m = 0)
}
# return the baseline instead of the baseline corrected spectra
x1_bl <-
ir::ir_sample_data |>
dplyr::slice(1:10) |>
ir::ir_bc(method = "rubberband", return_bl = TRUE)
Performs baseline correction on infrared spectra using a polynomial
Description
ir_bc_polynomial
performs baseline correction for infrared
spectra using a polynomial.
ir_bc_polynomial
is an extended wrapper function
for ChemoSpec::baselineSpectra()
.
Usage
ir_bc_polynomial(x, degree = 2, return_bl = FALSE, ...)
Arguments
x |
An object of class |
degree |
An integer value representing the degree of the polynomial used for baseline correction. |
return_bl |
A logical value indicating if for each spectrum the baseline
should be returned instead of the corrected intensity values
( |
... |
Ignored. |
Value
An object of class ir
with the baseline corrected spectra if
returnbl = FALSE
or the baselines if returnbl = TRUE
.
See Also
Examples
if(! requireNamespace("ChemoSpec", quietly = TRUE)) {
x2 <-
ir::ir_sample_data |>
ir::ir_bc_polynomial(degree = 2, return_bl = FALSE)
}
Performs baseline correction on infrared spectra using a rubberband algorithm
Description
ir_bc_rubberband
performs baseline correction for infrared spectra
using a rubberband algorithm. ir_bc_rubberband
is an extended wrapper
function for hyperSpec::spc.rubberband()
.
Usage
ir_bc_rubberband(x, do_impute = FALSE, return_bl = FALSE, ...)
Arguments
x |
An object of class |
do_impute |
A logical value indicating whether the in baseline the first
and last values should be imputed with the second first and second last
values, respectively ( |
return_bl |
A logical value indicating if for each spectrum the baseline
should be returned instead of the corrected intensity values
( |
... |
Ignored. |
Value
An object of class ir
with the baseline corrected spectra and,
if returnbl = TRUE
, the baselines.
See Also
Examples
x1 <-
ir::ir_sample_data |>
ir::ir_bc_rubberband(return_bl = FALSE)
Performs baseline correction on infrared spectra using a Savitzky-Golay baseline
Description
ir_bc_sg
computes a smoothed version of spectra using
ir_smooth()
with method = "sg"
and uses this as baseline
which is subtracted from the spectra to perform a baseline correction
(Lasch 2012).
Usage
ir_bc_sg(x, ..., return_bl = FALSE)
Arguments
x |
An object of class |
... |
Arguments passed to |
return_bl |
A logical value indicating if for each spectrum the baseline
should be returned instead of the corrected intensity values
( |
Value
An object of class ir
with the baseline corrected spectra and,
if returnbl = TRUE
, the baselines.
References
Lasch P (2012). “Spectral Pre-Processing for Biomedical Vibrational Spectroscopy and Microspectroscopic Imaging.” Chemometrics and Intelligent Laboratory Systems, 117, 100–114. ISSN 01697439, doi:10.1016/j.chemolab.2012.03.011.
Examples
if(! requireNamespace("signal", quietly = TRUE)) {
x <-
ir::ir_sample_data |>
ir::ir_bc_sg(p = 3, n = 199, ts = 1, m = 0, return_bl = FALSE)
}
Bins infrared spectra
Description
ir_bin
bins intensity values of infrared spectra into bins of a
defined width or into a defined number of bins.
Usage
ir_bin(x, width = 10, new_x_type = "start", return_ir_flat = FALSE)
Arguments
x |
An object of class |
width |
An integer value indicating the wavenumber width of each resulting bin. |
new_x_type |
A character value denoting how new wavenumber values for
the computed bins should be stored in the spectra of
|
return_ir_flat |
Logical value. If |
Details
If a wavenumber value exactly matches the boundary of a bin window, the respective intensity value will be assigned to both neighboring bins.
Value
An object of class ir
(or ir_flat
, if return_ir_flat = TRUE
),
where spectra have been binned.
Examples
# new wavenumber values are the first wavenumber value for each bin
x1 <-
ir::ir_sample_data |>
ir_bin(width = 50, new_x_type = "start")
# new wavenumber values are the last wavenumber value for each bin
x2 <-
ir::ir_sample_data |>
ir_bin(width = 50, new_x_type = "mean")
# new wavenumber values are the average of the wavenumber values assigned to
# each bin
x3 <-
ir::ir_sample_data |>
ir_bin(width = 50, new_x_type = "end")
# compare wavenumber values for first spectra.
cbind(x1$spectra[[1]]$x, x2$spectra[[1]]$x, x3$spectra[[1]]$x)
Clips infrared spectra to new wavenumber ranges
Description
ir_clip
clips infrared spectra to a new, specified, wavenumber range
or multiple new specified wavenumber ranges.
Usage
ir_clip(x, range, return_ir_flat = FALSE)
Arguments
x |
An object of class |
range |
A
If |
return_ir_flat |
Logical value. If |
Value
An object of class ir
(or ir_flat
, if return_ir_flat = TRUE
)
where spectra have been clipped.
Examples
## clipping with one range
# define clipping range
range <-
data.frame(start = 900, end = 1000)
# clip
x <-
ir::ir_sample_data |>
ir::ir_clip(range = range)
## clipping with mutliple ranges
# define clipping range
range <-
data.frame(start = c(900, 1900), end = c(1000, 2200))
# clip
x <-
ir::ir_sample_data |>
ir::ir_clip(range = range)
Corrects artifacts in a spectrum based on reference spectra of the artifact compound
Description
ir_correct_atmosphere
takes two objects of class ir
with the same number
of spectra in each and corrects the spectra of the first object with the
spectra of the second object according to the procedure presented by
(Perez-Guaita et al. 2013).
Usage
ir_correct_atmosphere(
x,
ref,
wn1,
wn2,
return_contribution = FALSE,
do_interpolate = FALSE,
start = NULL,
dw = 1,
warn = TRUE,
return_ir_flat = FALSE
)
Arguments
x |
An object of class |
ref |
An object of class |
wn1 |
A numeric value representing the first wavenumber value to use as reference point (Perez-Guaita et al. 2013). Examples used by Perez-Guaita et al. (2013) are:
|
wn2 |
A numeric value representing the second wavenumber value to use as reference point (Perez-Guaita et al. 2013). Examples used by Perez-Guaita et al. (2013) are:
|
return_contribution |
A logical value indicating whether in addition to
the corrected spectra, the computed relative contribution of |
do_interpolate |
A logical value indicating if |
start |
See |
dw |
See |
warn |
A logical value indicating whether warnings about mismatching
wavenumber values should be displayed ( |
return_ir_flat |
Logical value. If |
Value
x
corrected with the reference spectra in ref
.
Source
There are no references for Rd macro \insertAllCites
on this help page.
Examples
x1 <-
ir_correct_atmosphere(
ir_sample_data[1:5, ], ir_sample_data[1:5, ], wn1 = 2361, wn2 = 2349
)
x2 <-
ir_correct_atmosphere(
ir_sample_data[1:5, ], ir_sample_data[1:5, ], wn1 = 2361, wn2 = 2349,
return_contribution = TRUE
)
x2$contribution
Divide infrared spectra or divide infrared spectra by a numeric value
Description
ir_divide
takes two objects of class ir
, x
and
y
, and divides their intensity values, or it takes one object of
class ir
, x
, and one numeric value, y
, and divides
all intensity values in x
by y
.
Usage
ir_divide(x, y)
Arguments
x |
An object of class |
y |
An object of class |
Value
x
where for each spectrum intensity values are divided by
the respective intensity values in y
(if y
is an object of
class ir
), or where all intensity values are divided by y
if
y
is a numeric value.
Examples
# division with y as ir object
x1 <-
ir::ir_divide(ir::ir_sample_data, ir::ir_sample_data)
x2 <-
ir::ir_divide(ir::ir_sample_data, ir::ir_sample_data[1, ])
# division with y being a numeric value
x3 <-
ir::ir_divide(ir::ir_sample_data, y = 20)
# division with y being a numeric vector
x4 <-
ir::ir_divide(
ir::ir_sample_data,
seq(from = 0.1, to = 2, length.out = nrow(ir::ir_sample_data))
)
Drops the column spectra
from an object is of class ir
Description
ir_drop_spectra
removes the column spectra
from an object
of class ir
and removes the "ir"
class attribute.
Usage
ir_drop_spectra(x)
Arguments
x |
An object of class |
Value
x
without column spectra
and without "ir"
class
attribute.
Examples
ir::ir_sample_data |>
ir_drop_spectra()
Prepares ir
objects for export to csv
Description
Prepares ir
objects for export to csv
files. To export ir
objects to
csv
, the spectra
column has to be converted to an own data frame and be
exported separately from the metadata.
When preparing metadata for export, ir_export_prepare
takes care of
separating measurement units and measurement errors in columns of class
units::units, errors::errors, and quantities::quantities (see the
examples).
Usage
ir_export_prepare(
x,
what = "metadata",
measurement_id = as.character(seq_len(nrow(x)))
)
Arguments
x |
An object of class |
what |
A character value defining what to prepare for export.
If |
measurement_id |
A character vector an element for each row in |
Value
A data frame.
Note
This function superseded irp_export_prepare()
from the
'irpeat' package.
Examples
x_spectra <- ir_export_prepare(ir_sample_data[1:5, ], what = "spectra")
x_metadata <- ir_export_prepare(ir_sample_data[1:5, ], what = "metadata")
Cleans objects of class ir_flat
Description
ir_flatten_clean
takes an object of class ir_flat
and either
returns all non-empty spectra or all empty spectra as object of class
ir_flat
.
Usage
ir_flat_clean(x, return_empty = FALSE)
Arguments
x |
An object of class |
return_empty |
A logical value indicating if the empty spectra should be
returned ( |
Value
x
where empty spectra are dropped (if return_empty = TRUE
) or
only empty spectra are returned (return_empty = FALSE
).
Converts objects of class ir
to objects of class ir_flat
Description
ir_flatten
takes and object of class ir
, extracts the
spectra
column and combines the spectra into an object of class
ir_flat
. Metadata are not retained during flattening.
Usage
ir_flatten(x, measurement_id = as.character(seq_len(nrow(x))))
Arguments
x |
An object of class |
measurement_id |
A character vector an element for each row in |
Value
An object of class ir_flat
.
Examples
x_flat <-
ir::ir_sample_data |>
ir::ir_flatten()
Extracts intensities from spectra in an ir
object for specific spectral channels
Description
ir_get_intensity
extracts intensity values of spectra for specific
user-defined spectral channels ("x axis values", e.g. wavenumber values).
Usage
ir_get_intensity(x, wavenumber, warn = TRUE)
Arguments
x |
An object of class |
wavenumber |
A numeric vector with spectral channels ("x axis values", e.g. wavenumber values) for which to extract intensities. |
warn |
logical value indicating if warnings should be displayed
( |
Value
x
with an additional column intensity
.
x$intensity
is a list column with each element representing a
data.frame
with a row for each element in wavenumber
and two
columns:
- x
The "x axis values" extracted with
ir_get_wavenumberindex()
applied onwavenumber
and the corresponding spectrum inx
.- y
The extracted intensity values
.
Examples
x <-
ir::ir_sample_data |>
ir::ir_get_intensity(wavenumber = 1090)
Extracts selected spectra from an object of class ir
Description
ir_get_spectrum
extracts selected spectra from an object of class ir
.
Usage
ir_get_spectrum(x, what)
Arguments
x |
An object of class |
what |
A numeric vector with each element representing a row in |
Value
An integer vector with the same length as wavenumber
with the
row indices of x
corresponding to the wavenumber values in wavenumber
.
Examples
x <-
ir::ir_sample_data |>
ir::ir_get_spectrum(what = c(5, 9))
Gets the index of a defined wavenumber value for a spectrum
Description
ir_get_wavenumberindex
gets for a defined wavenumber value or set of
wavenumber values the corresponding indices (row number) in an object of
class ir
that has been flattened with ir_flatten()
. If the
specified wavenumber values do not match exactly the wavenumber values in the
ir
object, the indices for the next wavenumber values will be
returned, along with a warning.
Usage
ir_get_wavenumberindex(x, wavenumber, warn = TRUE)
Arguments
x |
A data.frame with a column x representing the x units of a spectrum
or several spectra (e.g. in the form of an object of class
|
wavenumber |
A numeric vector with wavenumber values for which to get indices. |
warn |
logical value indicating if warnings should be displayed
( |
Value
An integer vector with the same length as wavenumber
with the
row indices of x
corresponding to the wavenumber values in wavenumber
.
Examples
x_index_1090 <-
ir::ir_sample_data |>
ir::ir_flatten() |>
ir::ir_get_wavenumberindex(wavenumber = 1090)
Identifies empty spectra in an ir
object
Description
ir_identify_empty_spectra()
identifies empty spectra in an object of class
ir
. An empty spectrum is a spectrum which has no data values
(no rows) or where all intensity values (column y
) are NA
.
Usage
ir_identify_empty_spectra(x)
Arguments
x |
An object of class |
Value
A logical vector indicating for each spectrum in x
whether it is
empty (TRUE
) or not (FALSE
).
Examples
ir_identify_empty_spectra(ir::ir_sample_data)
Imports infrared spectra from various files
Description
ir_import_csv
imports raw infrared spectra from one or more .csv
file
that contains at least one spectrum, with x axis values (e.g. wavenumbers) in
the first column and intensity values of spectra in remaining columns. Note
that the function does not perform any checks for the validity of the content
read from the .csv file.
Usage
ir_import_csv(filenames, sample_id = "from_filenames", ...)
Arguments
filenames |
A character vector representing the complete paths to
the |
sample_id |
Either:
|
... |
Further arguments passed to
|
Value
An object of class ir
containing the
infrared spectra extracted from the .csv
file(s).
Examples
# import data from csv files
d <-
ir::ir_import_csv(
system.file(package = "ir", "extdata/klh_hodgkins_mir.csv"),
sample_id = "from_colnames"
)
Imports infrared spectra from Thermo Galactic's files
Description
ir_import_spc
imports raw infrared spectra from a Thermo Galactic's
.spc
file or several of such files. ir_import_spc
is a wrapper
function to hyperSpec::read.spc()
.
Usage
ir_import_spc(filenames, log.txt = TRUE)
Arguments
filenames |
A character vector representing the complete paths to
the |
log.txt |
A logical value indicating whether to import metadata ( |
Details
Currently, log.txt
must be set to FALSE
due to a bug in
hyperSpec::read.spc()
. This bug fill be fixed in the upcoming weeks and
currently can be circumvented by using the development version of 'hyperSpec'.
See https://github.com/r-hyperspec/hyperSpec/issues/80.
Value
An object of class ir
containing the
infrared spectra extracted from the .spc
file(s) and the metadata as
extracted by hyperSpec::read.spc()
. Metadata variables are:
- scan_number
An integer value representing the number of scans.
- detection_gain_factor
The detection gain factor.
- scan_speed
The scan speed [kHz].
- laser_wavenumber
The wavenumber of the laser.
- detector_name
The name of the detector.
- source_name
The name of the infrared radiation source.
- purge_delay
The duration of purge delay before a measurement [s].
- zero_filling_factor
A numeric value representing the zero filling factor.
- apodisation_function
The name of the apodisation function.
- exponentiation_factor
The exponentiation factor used for file compression.
- data_point_number
The number of data points in the spectrum
- x_variable_type
The type of the x variable.
- y_variable_type
The type of the y variable.
- measurement_date
A POSIXct representing the measurement date and time.
- measurement_device
The name of the measurement device.
Examples
# import a sample .spc file
x <-
ir::ir_import_spc(
system.file("extdata/1.spc", package = "ir"),
log.txt = FALSE
)
Interpolates intensity values of infrared spectra in an ir
object for new wavenumber values
Description
ir_interpolate
interpolates intensity values for infrared spectra for
new wavenumber values.
Usage
ir_interpolate(x, start = NULL, dw = 1, return_ir_flat = FALSE)
Arguments
x |
An object of class |
start |
A numerical value indicating the start wavenumber value relative
to which new wavenumber values will be interpolated. The value is not allowed
to be < |
dw |
A numerical value representing the desired wavenumber value difference between adjacent values. |
return_ir_flat |
Logical value. If |
Value
An object of class ir
(or ir_flat
, if return_ir_flat = TRUE
),
containing the interpolated spectra. Any NA
values resulting from
interpolation will be automatically dropped.
Examples
x <-
ir::ir_sample_data |>
ir::ir_interpolate(start = NULL, dw = 1)
Interpolates selected regions in infrared spectra in an ir
object
Description
ir_interpolate_region
linearly interpolates a user-defined region in
infrared spectra.
Usage
ir_interpolate_region(x, range)
Arguments
x |
An object of class |
range |
A
For each row in |
Value
x
with the defined wavenumber region(s) interpolated linearly.
Examples
# interpolation range
range <- data.frame(start = 1000, end = 1500)
# do the interpolation
x <-
ir::ir_sample_data |>
ir::ir_interpolate_region(range = range)
Multiply infrared spectra or multiply infrared spectra with a numeric value
Description
ir_multiply
takes two objects of class ir
, x
and
y
, and multiplies their intensity values, or it takes one object of
class ir
, x
, and one numeric value, y
, and multiplies
all intensity values in x
with y
.
Usage
ir_multiply(x, y)
Arguments
x |
An object of class |
y |
An object of class |
Value
x
where for each spectrum intensity values are multiplied with
the respective intensity values in y
(if y
is an object of
class ir
), or where all intensity values are multiplied with y
if y
is a numeric value.
Examples
# multiplication with y as ir object
x1 <-
ir::ir_multiply(ir::ir_sample_data, ir::ir_sample_data)
x2 <-
ir::ir_multiply(ir::ir_sample_data, ir::ir_sample_data[1, ])
# multiplication with y being a numeric value
x3 <-
ir::ir_multiply(ir::ir_sample_data, y = -1)
# multiplication with y being a numeric vector
x4 <-
ir::ir_multiply(
ir::ir_sample_data,
seq(from = 0, to = 2, length.out = nrow(ir::ir_sample_data))
)
Creates an object of class ir
Description
ir_new_ir
is the constructor function for objects of class
ir
.
An object of class ir
is a tibble::tbl_df()
with a
sample in each row and a list column containing spectra for each sample.
Usage
ir_new_ir(spectra, metadata = tibble::tibble())
Arguments
spectra |
A named list in which each element contains spectral data
for one measurement. Each list element must be a |
metadata |
An optional |
Value
An object of class ir
with the following columns:
- spectra
A list column identical to
spectra
.- ...
Additional columns contained in
metadata
.
Examples
ir_new_ir(
spectra = ir_sample_data$spectra,
metadata = ir_sample_data |> dplyr::select(-spectra)
)
Creates an object of class ir_flat
Description
ir_new_ir_flat
is the constructor function for objects of class ir_flat
.
An object of class ir_flat
is a data.frame
where the first
column ("x"
) contains unique x values of spectra (e.g. wavenumbers)
and all remaining columns represent intensity values from spectra
corresponding to the x values.
Usage
ir_new_ir_flat(x)
Arguments
x |
A |
Value
An object of class ir_flat
.
Examples
x_flat <-
ir::ir_sample_data |>
ir::ir_flatten()
Normalizes infrared spectra in an ir
object
Description
ir_normalize
normalizes the intensity values of infrared spectra. Different
methods for normalization are available.
Usage
ir_normalize(x, method = "area")
ir_normalise(x, method = "area")
Arguments
x |
An object of class |
method |
A character value specifying which normalization method to apply:
|
Value
An object of class ir
representing a normalized version of
x
.
Examples
# with method = "area"
x1 <-
ir::ir_sample_data |>
ir::ir_normalize(method = "area")
# second derivative spectrum with method = "area" or method = "area_absolute"
x2 <-
ir::ir_sample_data |>
ir::ir_smooth(method = "sg", n = 31, m = 2) |>
ir::ir_normalize(method = "area")
x3 <-
ir::ir_sample_data |>
ir::ir_smooth(method = "sg", n = 31, m = 2) |>
ir::ir_normalize(method = "area_absolute")
# with method = "zeroone"
x4 <-
ir::ir_sample_data |>
ir::ir_normalize(method = "zeroone")
# with method = "vector"
x5 <-
ir::ir_sample_data |>
ir::ir_normalize(method = "vector")
# with method = "snv"
x6 <-
ir::ir_sample_data |>
ir::ir_normalize(method = "snv")
# normalizing to a specific peak
x7 <-
ir::ir_sample_data |>
ir::ir_normalize(method = 1090)
Removes empty data values in an object of class ir
Description
ir_remove_missing
takes and object of class ir
and removes all
rows in the data.frame
s of the list column spectra
that have
NA
intensity values (column y
). Additionally, one can specify
to remove rows in the ir
object to discard if they contain empty
spectra.
Usage
ir_remove_missing(x, remove_rows = FALSE)
Arguments
x |
An object of class |
remove_rows |
A logical value indicating if rows in |
Value
x
with cleaned spectra.
Examples
# create sample data with some missing rows and one entire missing spectra
x <-
ir::ir_sample_data
x$spectra[[1]] <- x$spectra[[1]][0, ]
x$spectra[[2]][1:100, "y"] <- NA_real_
# remove missing values (but remove no rows in x)
x1 <-
x |>
ir::ir_remove_missing(remove_rows = FALSE)
# remove missing values (and remove rows in x if a compete spectrum is
# missing)
x2 <-
x |>
ir::ir_remove_missing(remove_rows = TRUE)
nrow(x)
nrow(x1)
nrow(x2)
Sample object of class ir
Description
A sample object of class ir
. The data set
contains ATR-MIR spectra for a set of organic reference materials
along with their metadata (types of samples and a description) and
accessory data (Klason lignin mass fraction and holocellulose mass fraction).
Usage
ir_sample_data
Format
A data frame with 58 rows and 7 variables:
- id_measurement
An integer vector with a unique id for each spectrum.
- id_sample
A character vector with a unique id for each sample.
- sample_type
A character vector containing class labels for the types of reference materials.
- sample_comment
A character vector containing comments to each sample.
- klason_lignin
A numeric vector with the mass fractions of Klason lignin in each sample.
- holocellulose
A numeric vector with the mass fractions of holocellulose in each sample.
- spectra
See
ir_new_ir()
.
Source
The data set was derived from https://www.nature.com/articles/s41467-018-06050-2 and published by Hodgkins et al. (2018) under the CC BY 4.0 license https://creativecommons.org/licenses/by/4.0/. Hodgkins et al. (2018) originally derived the data on Klason Lignin and Holocellulose content from De la Cruz et al. (2016).
References
De la Cruz FB, Osborne J, Barlaz MA (2016).
“Determination of Sources of Organic Matter in Solid Waste by Analysis of Phenolic Copper Oxide Oxidation Products of Lignin.”
Journal of Environmental Engineering, 142(2), 04015076.
ISSN 0733-9372, 1943-7870, doi:10.1061/(ASCE)EE.1943-7870.0001038.
Hodgkins SB, Richardson CJ, Dommain R, Wang H, Glaser PH, Verbeke B, Winkler BR, Cobb AR, Rich VI, Missilmani M, Flanagan N, Ho M, Hoyt AM, Harvey CF, Vining SR, Hough MA, Moore TR, Richard PJH, De La Cruz FB, Toufaily J, Hamdan R, Cooper WT, Chanton JP (2018).
“Tropical Peatland Carbon Storage Linked to Global Latitudinal Trends in Peat Recalcitrance.”
Nature Communications, 9(1), 3640.
ISSN 2041-1723, doi:10.1038/s41467-018-06050-2.
Wrapper to sampling functions from the 'prospectr' package
Description
Wrapper functions that allows to directly use 'ir' objects with sampling functions from the 'prospectr' package.
Usage
ir_sample_prospectr(x, sampling_function, ..., return_prospectr_output = FALSE)
Arguments
x |
An object of class 'ir' containing the spectra based on which to sample measurements. |
sampling_function |
A function from the 'prospectr' package to perform
sampling based on spectra ( |
... |
Arguments passed to |
return_prospectr_output |
Logical value. If |
Value
If return_prospectr_output = TRUE
, the output of sampling_function
. See
the 'prospectr' package for details. If return_prospectr_output = FALSE
,x
with the following additional columns:
- for_prospectr_model
Logical value indicating whether the spectrum is listed in element
model
of the prospectr output (TRUE
) or not (FALSE
).- for_prospectr_test
Logical value indicating whether the spectrum is listed in element
test
of the prospectr output (TRUE
) or not (FALSE
).- prospectr_model
Integer representing the order in which spectra are listed in element
model
of the prospectr output.- prospectr_test
Integer representing the order in which spectra are listed in element
test
of the prospectr output.
Examples
if(requireNamespace("prospectr", quietly = TRUE)) {
x <-
ir_sample_prospectr(
ir::ir_sample_data,
prospectr::kenStone,
metric = "euclid",
k = 30,
return_prospectr_output = FALSE
)
x <-
ir_sample_prospectr(
ir::ir_sample_data,
prospectr::kenStone,
metric = "euclid",
k = 30,
return_prospectr_output = TRUE
)
}
Scales spectra in an ir
object
Description
Scales spectra in an ir
object
Usage
ir_scale(x, center = TRUE, scale = TRUE, return_ir_flat = FALSE)
Arguments
x |
An object of class |
center |
either a logical value or numeric-alike vector of length
equal to the number of columns of |
scale |
either a logical value or a numeric-alike vector of length
equal to the number of columns of |
return_ir_flat |
Logical value. If |
Value
x
where spectra have been scaled, i.e. from each intensity value,
the average across all spectra is subtracted (when center
is a logical
value), or the respective value in center
is subtracted (when center
is
numerical), and each intensity value is divided by the standard deviation of
the intensity values at this wavenumber across all spectra (when scale
is a
logical value), or the respective value in scale
(when scale
is
numerical). NA
s are omitted during this process.
Examples
ir_sample_data |>
ir_scale() |>
plot()
Smooths infrared spectra in an ir
object
Description
ir_smooth
applies smoothing functions to infrared spectra.
ir_smooth
either performs Savitzky-Golay smoothing, using on
signal::sgolayfilt()
, or Fourier smoothing using
fda::smooth.basis()
. Savitzky-Golay smoothing can
also be used to compute derivatives of spectra.
Usage
ir_smooth(
x,
method = "sg",
p = 3,
n = p + 3 - p%%2,
ts = 1,
m = 0,
k = 111,
...
)
Arguments
x |
An object of class |
method |
A character value specifying which smoothing method to apply.
If |
p |
An integer value representing the filter order (i.e. the degree of
the polynom) of the Savitzky-Golay filter if |
n |
An odd integer value representing the length (i.e. the number of
wavenumber values used to construct the polynom) of the Savitzky-Golay filter
if |
ts |
time scaling factor. See |
m |
An integer value representing the mth derivative to compute. This
option can be used to compute derivatives of spectra. See
|
k |
A positive odd integer representing the number of Fourier basis
functions to use as smoothed representation of the spectra if
|
... |
additional arguments (ignored). |
Details
When x
contains spectra with different wavenumber values, the
filters are applied for each spectra only on existing wavenumber values. This
means that the filter window (if method == "sg"
) will be different for
these different spectra.
Value
x
with smoothed spectra.
Examples
#' # Savitzky-Golay smoothing
if(! requireNamespace("signal", quietly = TRUE)) {
x1 <-
ir::ir_sample_data[1:5, ] |>
ir::ir_smooth(method = "sg", p = 3, n = 51, ts = 1, m = 0)
}
# Fourier smoothing
if(! requireNamespace("fda", quietly = TRUE)) {
x2 <-
ir::ir_sample_data[1:5, ] |>
ir::ir_smooth(method = "fourier", k = 21)
}
# computing derivative spectra with Savitzky-Golay smoothing (here: first
# derivative)
if(! requireNamespace("signal", quietly = TRUE)) {
x3 <-
ir::ir_sample_data[1:5, ] |>
ir::ir_smooth(method = "sg", p = 3, n = 51, ts = 1, m = 1)
}
Stacks a matrix or data frame with spectra into a list column
Description
ir_stack
takes a matrix or data frame with infrared spectra and
converts it into a list column corresponding to the column spectra
in
objects of class ir
.
Usage
ir_stack(x)
Arguments
x |
A matrix or data frame with a first column ( |
Value
A tibble::tibble()
with the stacked spectra in column spectra
.
Examples
# from data frame
x1 <-
ir::ir_sample_data |>
ir::ir_flatten() |>
ir::ir_stack()
# from matrix
x2 <-
ir::ir_sample_data |>
ir::ir_flatten() |>
as.matrix() |>
ir::ir_stack()
Subtract infrared spectra
Description
ir_subtract
takes two objects of class ir
, x
and
y
, and subtracts the intensity values of spectra in matching rows from
y
from that of x
. Alternatively, takes an object of class
ir
, x
, and a numeric value, y
, and subtracts y
from all intensity values in x
.
Usage
ir_subtract(x, y)
Arguments
x |
An object of class |
y |
An object of class |
Value
x
where for each spectrum the respective intensity values in
y
are subtracted (if y
is an object of class ir
), or
where for each spectrum y
has been subtracted from the intensity
values.
Examples
# subtracting two objects of class ir
x1 <-
ir::ir_subtract(ir::ir_sample_data, ir::ir_sample_data)
x2 <-
ir::ir_subtract(ir::ir_sample_data, ir::ir_sample_data[1, ])
# subtracting a numeric value from an object of class `ir`.
x3 <-
ir::ir_subtract(ir::ir_sample_data, 20)
# subtracting a numeric vector from an object of class `ir`.
x4 <-
ir::ir_subtract(
ir::ir_sample_data,
seq(from = 0, to = 2, length.out = nrow(ir::ir_sample_data))
)
Converts absorbance spectra to transmittance spectra or vice versa
Description
ir_to_transmittance
converts absorbance spectra to transmittance
spectra. ir_to_absorbance
converts transmittance spectra to absorbance
spectra. Note that neither function checks whether the input spectra are
absorbance or transmittance spectra.
Usage
ir_to_transmittance(x)
ir_to_absorbance(x)
Arguments
x |
An object of class |
Value
x
with y values fore each spectrum as transmittance values
(in case of ir_to_transmittance
) or absorbance values (in case of
ir_to_absorbance
).
Source
(Stuart 2004).
References
Stuart BH (2004). Infrared Spectroscopy: Fundamentals and Applications, Analytical Techniques in the Sciences. John Wiley and Sons, Ltd, Chichester, UK. ISBN 978-0-470-01114-0 978-0-470-85428-0, doi:10.1002/0470011149.
Examples
# convert from absorbance to transmittance
x1 <-
ir_sample_data |>
ir_to_transmittance()
# convert from transmittance to absorbance
x2 <-
x1 |>
ir::ir_to_absorbance()
vapply(
seq_along(x2$spectra),
FUN = function(i) all.equal(x2$spectra[[i]], ir::ir_sample_data$spectra[[i]]),
FUN.VALUE = logical(1L)
) |>
all()
Computes the variance of a spectrum in an ir
object in a given region
Description
ir_variance_region
takes a spectrum x
and, depending on the
arguments computes the following summary:
- if
subtract_smoothed = FALSE
it computes the variance of the intensity values for each spectrum in
x
. If in additionrange
is notNULL
, it computes the variance only for the region(s) represented byrange
.- if
subtract_smoothed = TRUE
it smoothes
x
, subtracts the smoothedx
from the unsmoothedx
and computes the variance of the difference intensity values. If in additionrange
is notNULL
, it computes the variance only for the region(s) represented byrange
.
Usage
ir_variance_region(
x,
subtract_smoothed = FALSE,
do_normalize = FALSE,
normalize_method,
...,
range = NULL
)
Arguments
x |
An object of class |
subtract_smoothed |
A logical value. If |
do_normalize |
A logical value. If set to |
normalize_method |
See |
... |
Arguments passed to |
range |
See |
Value
x
with two additional columns:
- variance
A numeric vector with the computed variances of the intensity values for the respective spectra.
- n_variance
An integer vector with the number of intensity values used during computing the variance.
Examples
# Whole spectra variance
x1 <-
ir::ir_sample_data |>
ir::ir_variance_region(
subtract_smoothed = FALSE,
do_normalize = TRUE,
normalize_method = "area",
range = NULL
)
# Spectra variance, but only from a specific region
range <- data.frame(start = 2700, end = 2800)
x2 <-
ir::ir_sample_data |>
ir::ir_normalize(method = "area") |>
ir::ir_variance_region(
subtract_smoothed = FALSE,
do_normalize = TRUE,
normalize_method = "area",
range = range
)
# Spectra variance after subtracting a smoothed version of the spectra and
# only from a specific region
x3 <-
ir::ir_sample_data %>%
ir::ir_variance_region(
subtract_smoothed = TRUE,
do_normalize = FALSE,
range = range,
p = 3, n = 31, ts = 1, m = 0
)
Mutate an ir
object by adding new or replacing existing columns
Description
Mutate an ir
object by adding new or replacing existing columns
Usage
mutate.ir(
.data,
...,
.keep = c("all", "used", "unused", "none"),
.before = NULL,
.after = NULL
)
transmute.ir(.data, ...)
Arguments
.data |
An object of class |
... |
< The value can be:
|
.keep |
Control which columns from
|
.before , .after |
< |
Value
.data
with modified columns. If the spectra
column is dropped or
invalidated (see ir_new_ir()
), the ir
class is dropped, else the object
is of class ir
.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## mutate
dplyr::mutate(ir_sample_data, hkl = klason_lignin + holocellulose)
## transmute
dplyr::transmute(ir_sample_data, hkl = klason_lignin + holocellulose)
Mutating joins for an ir
object
Description
Mutating joins for an ir
object
Usage
inner_join.ir(
x,
y,
by = NULL,
copy = FALSE,
suffix = c(".x", ".y"),
...,
keep = FALSE,
na_matches = c("na", "never")
)
left_join.ir(
x,
y,
by = NULL,
copy = FALSE,
suffix = c(".x", ".y"),
...,
keep = FALSE,
na_matches = c("na", "never")
)
right_join.ir(
x,
y,
by = NULL,
copy = FALSE,
suffix = c(".x", ".y"),
...,
keep = FALSE,
na_matches = c("na", "never")
)
full_join.ir(
x,
y,
by = NULL,
copy = FALSE,
suffix = c(".x", ".y"),
...,
keep = FALSE,
na_matches = c("na", "never")
)
Arguments
x |
An object of class |
y |
A data frame. |
by |
A join specification created with If To join on different variables between To join by multiple variables, use a
For simple equality joins, you can alternatively specify a character vector
of variable names to join by. For example, To perform a cross-join, generating all combinations of |
copy |
If |
suffix |
If there are non-joined duplicate variables in |
... |
Other parameters passed onto methods. |
keep |
Should the join keys from both
|
na_matches |
Should two |
Value
x
and y
joined. If the spectra
column is renamed, the ir
class is dropped. See mutate-joins
.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## inner_join
set.seed(234)
dplyr::inner_join(
ir_sample_data,
tibble::tibble(
id_measurement = c(1:5, 101:105),
nitrogen_content = rbeta(n = 10, 0.2, 0.1)
),
by = "id_measurement"
)
## left_join
set.seed(234)
dplyr::left_join(
ir_sample_data,
tibble::tibble(
id_measurement = c(1:5, 101:105),
nitrogen_content = rbeta(n = 10, 0.2, 0.1)
),
by = "id_measurement"
)
## right_join
set.seed(234)
dplyr::right_join(
ir_sample_data,
tibble::tibble(
id_measurement = c(1:5, 101:105),
nitrogen_content = rbeta(n = 10, 0.2, 0.1)
),
by = "id_measurement"
)
## full_join
set.seed(234)
dplyr::full_join(
ir_sample_data,
tibble::tibble(
id_measurement = c(1:5, 101:105),
nitrogen_content = rbeta(n = 10, 0.2, 0.1)
),
by = "id_measurement"
)
Nest and un-nest an ir
object
Description
Nest and un-nest an ir
object
Usage
nest.ir(.data, ..., .names_sep = NULL, .key = deprecated())
unnest.ir(
data,
cols,
...,
keep_empty = FALSE,
ptype = NULL,
names_sep = NULL,
names_repair = "check_unique",
.drop = deprecated(),
.id = deprecated(),
.sep = deprecated(),
.preserve = deprecated()
)
Arguments
.data |
An object of class |
... |
< Specified using name-variable pairs of the form
If not supplied, then
|
.key |
The name of the resulting nested column. Only applicable when
If |
data |
A data frame. |
cols |
< When selecting multiple columns, values from the same row will be recycled to their common size. |
keep_empty |
By default, you get one row of output for each element
of the list that you are unchopping/unnesting. This means that if there's a
size-0 element (like |
ptype |
Optionally, a named list of column name-prototype pairs to
coerce |
names_sep , .names_sep |
If If a string, the inner and outer names will be used together. In
|
names_repair |
Used to check that output data frame has valid names. Must be one of the following options:
See |
.drop , .preserve |
|
.id |
|
.sep |
Value
.data
with nested or unnested columns. If the spectra
column is
dropped or invalidated (see ir_new_ir()
), the ir
class is dropped, else
the object is of class ir
.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate
,
mutate-joins
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## nest
ir_sample_data |>
tidyr::nest(
contents = c(holocellulose, klason_lignin)
)
## unnest
ir_sample_data |>
tidyr::nest(
contents = c(holocellulose, klason_lignin)
) |>
tidyr::unnest("contents")
Pivot an ir
object from wide to long
Description
Pivot an ir
object from wide to long
Usage
pivot_longer.ir(
data,
cols,
names_to = "name",
names_prefix = NULL,
names_sep = NULL,
names_pattern = NULL,
names_ptypes = list(),
names_transform = list(),
names_repair = "check_unique",
values_to = "value",
values_drop_na = FALSE,
values_ptypes = list(),
values_transform = list(),
...
)
Arguments
data |
An object of class |
cols |
< |
names_to |
A character vector specifying the new column or columns to
create from the information stored in the column names of
|
names_prefix |
A regular expression used to remove matching text from the start of each variable name. |
names_sep , names_pattern |
If
If these arguments do not give you enough control, use
|
names_ptypes , values_ptypes |
Optionally, a list of column name-prototype
pairs. Alternatively, a single empty prototype can be supplied, which will
be applied to all columns. A prototype (or ptype for short) is a
zero-length vector (like |
names_transform , values_transform |
Optionally, a list of column
name-function pairs. Alternatively, a single function can be supplied,
which will be applied to all columns. Use these arguments if you need to
change the types of specific columns. For example, If not specified, the type of the columns generated from |
names_repair |
What happens if the output has invalid column names?
The default, |
values_to |
A string specifying the name of the column to create
from the data stored in cell values. If |
values_drop_na |
If |
... |
Additional arguments passed on to methods. |
Value
data
in a long format. If the spectra
column is dropped
or invalidated (see ir_new_ir()
), the ir
class is dropped, else the
object is of class ir
.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate
,
mutate-joins
,
nest
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## pivot_longer
ir_sample_data |>
tidyr::pivot_longer(
cols = dplyr::any_of(c("holocellulose", "klason_lignin"))
)
Pivot an ir
object from wide to long
Description
Pivot an ir
object from wide to long
Usage
pivot_wider.ir(
data,
id_cols = NULL,
names_from = "name",
names_prefix = "",
names_sep = "_",
names_glue = NULL,
names_sort = FALSE,
names_repair = "check_unique",
values_from = "value",
values_fill = NULL,
values_fn = NULL,
...
)
Arguments
data |
An object of class |
id_cols |
< Defaults to all columns in |
names_from , values_from |
< If |
names_prefix |
String added to the start of every variable name. This is
particularly useful if |
names_sep |
If |
names_glue |
Instead of |
names_sort |
Should the column names be sorted? If |
names_repair |
What happens if the output has invalid column names?
The default, |
values_fill |
Optionally, a (scalar) value that specifies what each
This can be a named list if you want to apply different fill values to different value columns. |
values_fn |
Optionally, a function applied to the value in each cell
in the output. You will typically use this when the combination of
This can be a named list if you want to apply different aggregations
to different |
... |
Additional arguments passed on to methods. |
Value
data
in a wide format. If the spectra
column is dropped
or invalidated (see ir_new_ir()
), the ir
class is dropped, else the
object is of class ir
.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## pivot_wider
ir_sample_data |>
tidyr::pivot_longer(
cols = dplyr::any_of(c("holocellulose", "klason_lignin"))
) |>
tidyr::pivot_wider(names_from = "name", values_from = "value")
Plots an object of class ir
Description
plot.ir
is the plot method for objects of class ir
.
Usage
## S3 method for class 'ir'
plot(x, ...)
Arguments
x |
An object of class |
... |
Further arguments, will be ignored. |
Value
An object of class ggplot2
.
Examples
# simple plotting
plot(ir::ir_sample_data[1:2, ])
# advanced functions
plot(ir::ir_sample_data) +
ggplot2::facet_wrap(~ sample_type)
Get the minima/maxima/range/median of x axis values or intensity values of infrared spectra
Description
range.ir
extracts the range of x axis values (e.g. wavenumbers) or
intensity values of infrared spectra.
min.ir
extracts the minimum x axis value (e.g. wavenumber) or
intensity value of infrared spectra.
max.ir
extracts the maximum x axis value (e.g. wavenumber) or
intensity value of infrared spectra.
median.ir
extracts the median x axis value (e.g. wavenumber) or
intensity value of infrared spectra.
Usage
## S3 method for class 'ir'
range(
x,
...,
na.rm = FALSE,
.dimension = "y",
.col_names = c("y_min", "y_max")
)
## S3 method for class 'ir'
min(x, ..., na.rm = FALSE, .dimension = "y", .col_name = "y_min")
## S3 method for class 'ir'
max(x, ..., na.rm = FALSE, .dimension = "y", .col_name = "y_max")
## S3 method for class 'ir'
median(x, na.rm = FALSE, ..., .dimension = "y", .col_name = "y_median")
Arguments
x |
An object of class |
... |
Further arguments, ignored. |
na.rm |
A logical value. See |
.dimension |
A character value. Must be one of the following:
|
.col_names |
A character vector of length 2 representing the names of the columns in which to store the extracted values. The first element is the name for the column with minima values, the second the name for the column with maxima values. |
.col_name |
A character value representing the name of the column in which to store the extracted values. |
Value
x
with the extracted values.
Examples
# range of intensity values
x1 <-
ir::ir_sample_data |>
range(.dimension = "y")
# minimum intensity values
x1 <-
ir::ir_sample_data |>
min(.dimension = "y")
# maximum intensity values
x1 <-
ir::ir_sample_data |>
max(.dimension = "y")
# median intensity values
x1 <-
ir::ir_sample_data |>
stats::median(.dimension = "y")
Rename columns in ir
objects
Description
Rename columns in ir
objects
Usage
rename.ir(.data, ...)
rename_with.ir(.data, .fn, .cols = dplyr::everything(), ...)
Arguments
.data |
An object of class |
... |
For For |
.fn |
A function used to transform the selected |
.cols |
< |
Value
.data
with renamed columns. If the spectra
column is renamed,
and no new valid spectra
column is created, the ir
class is dropped, else
the object is of class ir
.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## rename
dplyr::rename(ir_sample_data, hol = "holocellulose")
dplyr::rename(ir_sample_data, spec = "spectra") # drops ir class
## rename_with
dplyr::rename_with(ir_sample_data, .cols = dplyr::starts_with("id_"),
toupper)
dplyr::rename_with(ir_sample_data, toupper) # drops ir class
Replicate ir objects
Description
rep.ir
is the replicate method for ir
objects.
Replicating and ir
object means to replicate its rows and bind these
together to a larger ir
object.
Usage
## S3 method for class 'ir'
rep(x, ...)
Arguments
x |
An object of class |
... |
See |
Value
An object of class ir
with replicated spectra.
Examples
# replicate the sample data
x <- rep(ir::ir_sample_data, times = 2)
x <- rep(ir::ir_sample_data, each = 2)
x <- rep(ir::ir_sample_data, length.out = 3)
Group input ir
objects by rows
Description
Group input ir
objects by rows
Usage
rowwise.ir(.data, ...)
Arguments
.data |
Input data frame. |
... |
< NB: unlike |
data |
An object of class |
Value
data
as row-wise data frame. See dplyr::rowwise()
.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## rowwise
dplyr::rowwise(ir_sample_data) |>
dplyr::mutate(
hkl =
mean(
units::drop_units(klason_lignin),
units::drop_units(holocellulose)
)
)
Subset columns in ir
objects using column names and types
Description
Subset columns in ir
objects using column names and types
Usage
select.ir(.data, ...)
Arguments
.data |
An object of class |
... |
< |
Value
.data
with the selected columns. If the spectra
column is dropped,
the ir
class is dropped, else the object is of class ir
.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## select
dplyr::select(ir_sample_data, spectra)
dplyr::select(ir_sample_data, holocellulose) # drops ir class
Separate a character column in an ir
object into multiple columns with a regular expression or numeric locations
Description
Separate a character column in an ir
object into multiple columns with a regular expression or numeric locations
Usage
separate.ir(
data,
col,
into,
sep = "[^[:alnum:]]+",
remove = TRUE,
convert = FALSE,
extra = "warn",
fill = "warn",
...
)
Arguments
data |
An object of class |
col |
< |
into |
Names of new variables to create as character vector.
Use |
sep |
Separator between columns. If character, If numeric, |
remove |
If |
convert |
If NB: this will cause string |
extra |
If
|
fill |
If
|
... |
Additional arguments passed on to methods. |
Value
.data
with separated columns. If the spectra
column is
dropped or invalidated (see ir_new_ir()
), the ir
class is dropped, else
the object is of class ir
.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate_rows.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## separate
ir_sample_data |>
tidyr::separate(
col = "id_sample", c("a", "b", "c")
)
Separate a collapsed column in an ir
object into multiple rows
Description
Separate a collapsed column in an ir
object into multiple rows
Usage
separate_rows.ir(data, ..., sep = "[^[:alnum:].]+", convert = FALSE)
Arguments
data |
An object of class |
... |
< |
sep |
Separator delimiting collapsed values. |
convert |
If |
Value
data
with a collapsed column separated into multiple rows. See
tidyr::separate_rows()
.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
slice
,
summarize
,
unite.ir()
Examples
## separate_rows
ir_sample_data |>
tidyr::unite(
col = content, holocellulose, klason_lignin
) |>
tidyr::separate_rows(
col
)
Subset rows in ir
objects using their positions
Description
Subset rows in ir
objects using their positions
Usage
slice.ir(.data, ..., .preserve = FALSE)
slice_sample.ir(.data, ..., n, prop, weight_by = NULL, replace = FALSE)
Arguments
.data |
An object of class |
... |
For Provide either positive values to keep, or negative values to drop. The values provided must be either all positive or all negative. Indices beyond the number of rows in the input are silently ignored. For |
.preserve |
Relevant when the |
n , prop |
Provide either A negative value of |
weight_by |
< |
replace |
Should sampling be performed with ( |
Value
.data
with subsetted rows.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
summarize
,
unite.ir()
Examples
## slice
dplyr::slice(ir_sample_data, 1:5)
dplyr::slice_min(ir_sample_data, holocellulose, n = 3)
dplyr::slice_max(ir_sample_data, holocellulose, n = 3)
dplyr::slice_head(ir_sample_data, n = 5)
dplyr::slice_tail(ir_sample_data, n = 5)
## slice_sample
set.seed(234)
dplyr::slice_sample(ir_sample_data, n = 3)
Subsetting ir
objects
Description
Subsetting ir
objects
Usage
## S3 method for class 'ir'
x[i, j, ..., exact = TRUE]
## S3 method for class 'ir'
x$i
## S3 method for class 'ir'
x[[i, j, ..., exact = TRUE]]
## S3 replacement method for class 'ir'
x$i, j, ... <- value
## S3 replacement method for class 'ir'
i[j, ..., exact = TRUE] <- value
## S3 replacement method for class 'ir'
i[[j, ..., exact = TRUE]] <- value
Arguments
x |
An object of class |
i , j |
Row and column indices. If |
... |
Ignored. |
exact |
Ignored, with a warning. |
value |
A value to store in a row, column, range or cell. Tibbles are stricter than data frames in what is accepted here. |
Value
If the subsetting operation preserves a valid spectra
column
(see ir()
), an object of class ir
with
accordingly subsetted rows or columns. Else a tibble::tbl_df()
or
vector.
Examples
# subsetting rows
ir_sample_data[1, ]
ir_sample_data[10:15, ]
ir_sample_data[ir_sample_data$sample_type == "office paper", ]
# subsetting columns
ir_sample_data[, "spectra"]
ir_sample_data[["spectra"]]
ir_sample_data$spectra
# not explicitly selecting the spectra column drops the ir class
class(ir_sample_data[, 1])
class(ir_sample_data[, "spectra"])
# subsetting values
ir_sample_data[, 1] # drops the ir class
ir_sample_data[, c("id_sample", "spectra")]
ir_sample_data$id_sample
ir_sample_data[[1, 1]]
# setting and replacing columns
x <- ir::ir_sample_data
x$a <- 3
x[, "a"] <- 4
x$sample_type <- "a"
x[[1]] <- rev(x[[1]])
# deleting the spectra column drops the ir class
x$spectra <- NULL
class(x)
# setting and replacing rows
x <- ir::ir_sample_data
x[1, ] <- x[2, ]
class(x)
# setting invalid values in the spectra column drops the ir class
x_replacement <- x[1, ]
x_replacement$spectra <- list(1)
x[1, ] <- x_replacement
class(x)
# setting and replacing values
x <- ir::ir_sample_data
x[[1, 1]] <- 100
# replacing an element in the spectra column by an invalid element drops the
# ir class attribute
x[[3, "spectra"]] <- list(1)
class(x)
Summarize each group in a ir
object to fewer rows
Description
Summarize each group in a ir
object to fewer rows
Usage
summarize.ir(.data, ..., .groups = NULL)
summarise.ir(.data, ..., .groups = NULL)
Arguments
.data |
An object of class |
... |
< The value can be:
|
.groups |
When
In addition, a message informs you of that choice, unless the result is ungrouped,
the option "dplyr.summarise.inform" is set to |
Value
.data
with summarized columns. If the spectra
column is dropped
or invalidated (see ir_new_ir()
), the ir
class is dropped, else the
object is of class ir
.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
unite.ir()
Examples
## summarize
# select in each sample_type groups the first spectrum
ir_sample_data |>
dplyr::group_by(sample_type) |>
dplyr::summarize(spectra = spectra[[1]])
Unite multiple columns in an ir
object into one by pasting strings together
Description
Unite multiple columns in an ir
object into one by pasting strings together
Usage
unite.ir(data, col, ..., sep = "_", remove = TRUE, na.rm = FALSE)
Arguments
data |
An object of class |
col |
The name of the new column, as a string or symbol. This argument is passed by expression and supports
quasiquotation (you can unquote strings
and symbols). The name is captured from the expression with
|
... |
< |
sep |
Separator to use between values. |
remove |
If |
na.rm |
If |
Value
.data
with united columns. If the spectra
column is
dropped or invalidated (see ir_new_ir()
), the ir
class is dropped, else
the object is of class ir
.
Source
See Also
Other tidyverse:
arrange.ir()
,
distinct.ir()
,
extract.ir()
,
filter-joins
,
filter.ir()
,
group_by
,
mutate
,
mutate-joins
,
nest
,
pivot_longer.ir()
,
pivot_wider.ir()
,
rename
,
rowwise.ir()
,
select.ir()
,
separate.ir()
,
separate_rows.ir()
,
slice
,
summarize
Examples
## unite
ir_sample_data |>
tidyr::separate(
"id_sample", c("a", "b", "c")
) |>
tidyr::unite(id_sample, a, b, c)