Functions to bind hyperSpec objects.

cbind2() binds the spectral matrices of two hyperSpec objects by column. All columns besides spc with the same name in x@data and y@data must have the same elements. Rows are ordered before checking.

rbind2() binds two hyperSpec objects by row. They need to have the same columns.

bind(
  direction = stop("direction('c' or 'r') required"),
  ...,
  wl.tolerance = hy_get_option("wl.tolerance")
)

# S3 method for hyperSpec
cbind(...)

# S3 method for hyperSpec
rbind(...)

# S4 method for hyperSpec,hyperSpec
cbind2(x, y)

# S4 method for hyperSpec,missing
cbind2(x, y)

# S4 method for hyperSpec,hyperSpec
rbind2(x, y, wl.tolerance = hy_get_option("wl.tolerance"))

# S4 method for hyperSpec,missing
rbind2(x, y, wl.tolerance)

Arguments

direction

"r" or "c" to bind rows or columns.

...

The hyperSpec objects to be combined.
Alternatively, one list of hyperSpec objects can be given to bind().

wl.tolerance

rbind and rbind2 check for equal wavelengths with this tolerance.

x, y

hyperSpec objects

Value

A hyperSpec object, possibly with different row order (for bind("c", ...) and cbind2()).

Details

The former difficulties with binding S4 objects are resolved since R version 3.2.0 and cbind() and rbind() now work as intended and expected for hyperSpec objects.

Therefore, calling rbind.hyperSpec and cbind.hyperSpec is now deprecated: cbind and rbind should now be called directly.

However, in consequence it is no longer possible to call cbind() or rbind() with a list of hyperSpec objects. In that case, use bind() or base::do.call() (see example).

bind() does the common work for both column- and row-wise binding.

Note

You might have to make sure that the objects either all have or all do not have rownames and/or colnames.

See also

methods::rbind2(), methods::cbind2(), base::rbind(), base::cbind(),

merge() and collapse() for combining objects that do not share spectra or wavelengths, respectively.

Author

C. Beleites

Examples


faux_cell
#> hyperSpec object
#>    875 spectra
#>    4 data columns
#>    300 data points / spectrum

bind("r", faux_cell, faux_cell)
#> hyperSpec object
#>    1750 spectra
#>    4 data columns
#>    300 data points / spectrum
rbind(faux_cell, faux_cell)
#> hyperSpec object
#>    1750 spectra
#>    4 data columns
#>    300 data points / spectrum
cbind(faux_cell, faux_cell)
#> hyperSpec object
#>    875 spectra
#>    4 data columns
#>    600 data points / spectrum
bind("r", list(faux_cell, faux_cell, faux_cell))
#> hyperSpec object
#>    2625 spectra
#>    4 data columns
#>    300 data points / spectrum

x <- faux_cell[, , 600:605]
x$a <- 1
x@data <- x@data[, sample(ncol(x), ncol(x))] # reorder columns

y <- faux_cell[nrow(faux_cell):1, , 1730:1750] # reorder rows
y$b <- 2

cbind2(x, y) # works
#> hyperSpec object
#>    875 spectra
#>    6 data columns
#>    27 data points / spectrum

y$y[3] <- 5
try(cbind2(x, y)) # error
#> hyperSpec object
#>    875 spectra
#>    6 data columns
#>    27 data points / spectrum

# list of hyperSpec objects

lhy <- list(flu, flu)
do.call("rbind", lhy)
#> hyperSpec object
#>    12 spectra
#>    3 data columns
#>    181 data points / spectrum
bind("r", lhy)
#> hyperSpec object
#>    12 spectra
#>    3 data columns
#>    181 data points / spectrum