0

I have spectral data that I am trying to run PCA on. To learn how to do this I have created a matrix with two distinct groups and then pulled a file in that has typical wavelengths. I am pre-processing the data and have run into issues with baseline correction. I can do it with a for loop, but want to know if I can do it with apply instead. I get different errors depending on what I am trying and don't know if it is even possible. My most recent error is:

Error in matrix(0, np[1], np[2]) : non-numeric matrix extent.

I get this immediately after:

playdata.baseline<-apply(playdata,1, baseline,lambda=1,hwi=20,it=30,int=800,method='fillPeaks')

Can I use apply for the baseline function? If yes, why is it not working with the rest of the code that works fine using a for loop for baseline?

Here is what works:

#baseline corrections
playdata.baseline=matrix(0,ncol=nrow(playdata),nrow=ncol(playdata))  
playdata.bc=c()
playdata=t(playdata)
for (n in 1:(length(playdata[,1]))){           
  playdata.bc=baseline(playdata[n,,drop=FALSE], 
                       lambda=1, hwi=20, it=30, 
                       int=800, method='fillPeaks')
  playdata.baseline[n,]=playdata.bc@corrected
}
playdata=playdata.baseline
playdata=t(playdata)

Keeping everything the same until # baseline corrections, the apply attempt is as follows:

#baseline corrections
playdata=t(playdata)
playdata.baseline <- apply(playdata, 1, baseline, lambda=1, hwi=20, 
                           it=30, int=800, method='fillPeaks')
playdata=t(playdata)
Shaune
  • 13
  • 3
  • Not an answer to your question, but depending upon where you are going with the overall analysis, you might find the [ChemoSpec](http://cran.at.r-project.org/web/packages/ChemoSpec/index.html) package a help. Disclaimer: I am the author. – Bryan Hanson May 01 '15 at 02:23
  • 1
    Can you trim your example down to a minimum reproducible [example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610), and show the exact steps that lead to the error? Beyond that, the error is telling you `np[1]` and/or `np[2]` are not integers (and they are not included in your code). – Bryan Hanson May 01 '15 at 02:27

0 Answers0