Learn R Programming

ir (version 0.4.1)

subsetting: Subsetting ir objects

Description

Subsetting ir objects

Usage

# S3 method for ir
[(x, i, j, ..., exact = TRUE)

# S3 method for ir $(x, i)

# S3 method for ir [[(x, i, j, ..., exact = TRUE)

# S3 method for ir $(x, i, j, ...) <- value

# S3 method for ir [(i, j, ..., exact = TRUE) <- value

# S3 method for ir [[(i, j, ..., exact = TRUE) <- value

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.

Arguments

x

An object of class ir.

i, j

Row and column indices. If j is omitted, i is used as column index.

...

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.

Examples

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

Run the code above in your browser using DataLab