Importing csv and making spectra object with all information

101 Views Asked by At

I have a FTIR data set that has wavenumber in column 1 and all of the sample intensity / absorbance values in the other columns. Looks like this

> FTIR<- read.csv(file="FTIR_20190328_test.csv", header = T, row.names = 1)
> head(FTIR)
               ID_3        ID_9       ID_21       ID_25      ID_29       ID_33
399.6748 0.05554183 0.072609851 0.039330115 0.055516636 0.03709701 0.055186592
400.1569 4.42064846 0.005875182 0.007921505 0.000000000 0.03182379 0.012444198
400.639  3.32078310 0.002113590 0.012585608 0.002157767 0.03158090 0.007927620
401.1211 2.22447073 0.000000000 0.017728826 0.004574567 0.03180063 0.004792073
401.6032 1.14204819 0.004404642 0.025178901 0.008065509 0.03402003 0.009207404
402.0854 0.06865115 0.012965923 0.034695270 0.012274070 0.03753230 0.019088973

I'm trying to create a Spectra object and use the Chemospec package in R. This is code I found in instructions.

# Get the data matrix
specs <- read.table(file = "FTIR_20190328_test.csv", sep = ",", header = T, row.names = 1)

# Set up the Spectra object
spectra <- list()
spectra$freq <- as.numeric(specs[,1])
spectra$data <- as.matrix(t(specs[,-1]))
dimnames(spectra$data) <- NULL
spectra$names <- names(specs[-1])
class(spectra) <- "Spectra"

# Go get group assignments & colors, to complete assembly of spectra

spectra <- .groupNcolor(spectra, gr.crit, gr.cols)
spectra$unit[1] <- freq.unit
spectra$unit[2] <- int.unit
spectra$desc <- descrip

if(chk) chkSpectra(spectra)

datafile <- paste(out.file, ".RData", sep = "")

R.utils::saveObject(spectra, file = datafile)

return(spectra)

I think I made the spectra object but it doesn't seem complete. When you use the test data set used in CRAN intro file (https://cran.r-project.org/web/packages/ChemoSpec/vignettes/ChemoSpec.pdf) the spectra object is much more complete. Theirs: enter image description here Mine: enter image description here

This block of code gives me an error:

spectra <- .groupNcolor(spectra, gr.crit, gr.cols)
spectra$unit[1] <- freq.unit
spectra$unit[2] <- int.unit
spectra$desc <- descrip

Here is error:

> spectra <- .groupNcolor(spectra, gr.crit, gr.cols)
Error in gr.cols[1] %in% builtInColors : object 'gr.cols' not found
> spectra$unit[1] <- freq.unit
Error: object 'freq.unit' not found
> spectra$unit[2] <- int.unit
Error: object 'int.unit' not found
> spectra$desc <- descrip
Error: object 'descrip' not found

How do I complete this spectra object so that it can be used in Chemospec. The directions don't explain what the matrix / spectra object should entail and the example doc of what their data would have looked like was not provided.

Ultimately, I am trying to visualize FTIR data but I can't even get the data in to mess with it.

Thank you in advance for any help.

I have tried to create spectra with the matrix2SpectraObject command but I'm not even 100% I'm entering the correct information in there. I am literally just trying to get the data into R in the appropriate format to use Chemospec, which is apparently a powerful tool for FTIR visualization.

0

There are 0 best solutions below