Search code examples
rmatrixpackagetxtbaseline

How to use R baseline package with txt file as input


I want to use the baseline package in R to correct my RAMAN spectra.

My spectra data are in txt files without headers. I know the baseline package needs a matrix with all data in one row. I tried to convert my files and use the baseline package, but it didn't work. How can I use them as the input data for the package which needs a matrix and how can I get the corrected data out in a txt or csv file again? Thank you

I tried using:

library(baseline)

data <- read.delim("file.txt",sep = "\t", header=FALSE)
data.matrix <- t(data)
corrected_data <- baseline(data.matrix[1,,drop=FALSE], method='irls')`
plot(corrected_data)

Even so I don't get an error message, I see on the plots that I'm getting nonsense. The plot of the initial data is already wrong.


Solution

  • Your textfile needs the following structure:

    • one row per spectrum
    • one column per band
    • a header, indicating the band (position along the x-axis), which can be converted to numeric.

    Have a look at the structure of the example data provided with {baseline}:

    library(baseline)
    
    data(milk)
    milk$spectra[1:5, 1:3]
    

    see how the column names provide the band, starting from 4999.9... :

         4999.94078628963 5001.55954267662 5003.17856106153
    [1,]          1028.75           923.50           826.00
    [2,]           370.75           368.25           331.25
    [3,]           606.50           555.75           534.25
    [4,]           367.50           350.00           325.00
    [5,]           553.50           500.75           451.75
    
    • let´s save a small portion of milk$spectra as a surrogate for our own data:
    write.csv(milk$spectra[1:5, 1:100], file = 'original.csv')
    
    • read them in and convert to matrix:
    spectra <- read.csv('original.csv', header = TRUE) |> 
      as.matrix()
    

    column names are prefixed with 'X' now, which doesn't matter though:

    > spectra[1:5, 1:3]
         X X4999.94078628963 X5001.55954267662
    [1,] 1           1028.75            923.50
    [2,] 2            370.75            368.25
    [3,] 3            606.50            555.75
    [4,] 4            367.50            350.00
    [5,] 5            553.50            500.75
    

    request baselines for the first four spectra (rows) and store it as bc:

    bc <- baseline(spectra[1:4, , drop = FALSE])
    

    inspect:

    plot(bc)
    

    corrected spectra

    retrieve the corrected spectra and store as csv:

    getCorrected(bc) |> write.csv(file = 'corrected.csv')