0

I am relatively new to coding and R in general. I am trying to do baseline correction for Raman data I have. My data is in two rows, an x and y-axis. I believe that the data needs to be a matrix and thus the reason why it is not working? I have tried to follow various guides to get it working. This is the code. Please try and help me where I am going wrong. The setwd is linked to the correct place but have removed.

library(baseline)
library(inspectr)
library(spectrolab)

setwd("")
exam = read.delim("top.txt", header=F)
mattt = as.matrix(exam)
bc.irls = baseline(mattt["spectra"][1,, drop=FALSE])
#bl3 <- baseline(spectra(mattt), method = 'irls')
plot(bc.irls)

The original method I am following

# Load data 
data(milk) 
# The baseline() function is an S4 wrapper for all the different 
# baseline correction methods. The default correction method 
# is IRLS. Data must be organized as row vectors in a matrix 
# or data.frame. 
bc.irls <- baseline(milk$spectra[1,, drop=FALSE]) 
## Not run: 
# Computationally heavy 
plot(bc.irls)

Thank you!

  • Following the example, why wouldn't `baseline(exam$spectra[1,,drop=FALSE])` work? – DaveArmstrong May 04 '22 at 12:25
  • That is what I originally thought, but the error '$ operator is invalid for atomic values' is returned. I have tried to fix this using https://stackoverflow.com/questions/23299684/r-error-in-xed-operator-is-invalid-for-atomic-vectors But this hasn't worked – mey48328943 May 04 '22 at 14:38
  • What if you do `exam <- as.data.frame(exam)` first? – DaveArmstrong May 04 '22 at 16:23
  • The data set needs to be a matrix though rather than a data frame. Ok, so I think I have got somewhere. ``` class(lok2) #matrix array bc.irls = baseline(lok2[1:2116, 1:2, drop=FALSE], method = 'irls') plot(bc.irls) ``` Think all I need to do is call the matrix now. I don't really know how to do this though. My data is 2 columns by 2116 rows. I just need to call it. I think – mey48328943 May 04 '22 at 17:29
  • Have you thought about using Python? Libraries like **spectrappeper** or **scipy** would help you with this tremendously. – Enric Grau-Luque May 18 '22 at 09:50
  • Did you try: `bc.irls = baseline(mattt[1,, drop=FALSE])`? – Ehuber Sep 19 '22 at 13:00

0 Answers0