Learn R Programming

hyperSpec (version 0.98-20140523)

as.data.frame: Conversion of a hyperSpec object into a data.frame or matrix as.data.frame returns x@data (as data.frame) as.matrix returns the spectra matrix x@data$spc as matrix

Description

Conversion of a hyperSpec object into a data.frame or matrix as.data.frame returns x@data (as data.frame) as.matrix returns the spectra matrix x@data$spc as matrix

as.wide.df converts the spectra matrix to a data.frame. The extra data together with this data is returned. The column names of the spectra matrix are retained (if they are numbers, without preceeding letters).

The data.frame returned by as.long.df is guaranteed to have columns spc and .wavelength. If nwl (x) == 0 these columns will be NA.

as.t.df produces a 'transposed' data.frame with columns containing the spectra.

Usage

## S3 method for class 'hyperSpec':
as.data.frame(x, row.names = TRUE,
    optional = NULL, ...)

## S3 method for class 'hyperSpec': as.matrix(x, ...)

as.wide.df(x)

as.long.df(x, rownames = FALSE, wl.factor = FALSE, na.rm = TRUE)

as.t.df(x)

Arguments

x
a hyperSpec object
row.names
if TRUE, a column .row is created containing row names or row indices if no rownames are set. If character vector, the rownames are set accordingly.
optional
ignored
...
ignored
rownames
should the rownames be in column .rownames of the long-format data.frame?
wl.factor
should the wavelengths be returned as a factor (instead of numeric)?
na.rm
if TRUE, rows where spc is not NA are deleted.

Value

  • x@data and x@data$spc (== x$spc == x [[]]), respectively.

    as.wide.df returns a data.frame that consists of the extra data and the spectra matrix converted to a data.frame. The spectra matrix is expanded in place.

    as.long.df returns the stacked or molten version of x@data. The wavelengths are in column .wavelength.

    as.t.df returns a data.frame similar to as.long.df, but each spectrum in its own column. This is useful for exporting summary spectra, see the example.

See Also

as.data.frame

and as.matrix

[ for a shortcut to as.matrix

stack and melt or melt for other functions producing long-format data.frames.

Examples

Run this code
as.data.frame (chondro [1:3,, 600 ~ 620])
as.matrix (chondro [1:3,, 600 ~ 620])
lm (c ~ spc, data = flu [,,450])
as.wide.df (chondro [1:5,, 600 ~ 610])
summary (as.wide.df (chondro [1:5,, 600 ~ 610]))
as.long.df (flu [,, 405 ~ 410])
summary (as.long.df (flu [,, 405 ~ 410]))
summary (as.long.df (flu [,, 405 ~ 410], rownames = TRUE))
summary (as.long.df (flu [,, 405 ~ 410], wl.factor = TRUE))
df <- as.t.df (apply (chondro, 2, mean_pm_sd))
head (df)

if (require (ggplot2)){
  ggplot (df, aes (x = .wavelength)) +
    geom_ribbon (aes (ymin = mean.minus.sd, ymax = mean.plus.sd),
      fill = "#00000040") +
    geom_line (aes (y = mean))
}

Run the code above in your browser using DataLab