Learn R Programming

ir (version 0.4.1)

ir_as_ir: 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 ir ir_as_ir(x, ...)

# S3 method for data.frame ir_as_ir(x, ...)

# S3 method for ir_flat ir_as_ir(x, ...)

# S3 method for hyperSpec ir_as_ir(x, ...)

# S3 method for Spectra ir_as_ir(x, ...)

Value

An object of class ir with available metadata from original objects.

Arguments

x

An object.

...

Further arguments passed to individual methods.

  • If x is a data frame, an object of class ir, an object of class hyperSpec (from package 'hyperSpec'), or an object of class Spectra (from package 'ChemoSpec'), these are ignored.

Examples

Run this code
# 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)
}

Run the code above in your browser using DataLab