This code is a script in the R programming language for performing baseline correction on multiple spectra stored in an Excel file. The script first loads the necessary libraries, including readxl
for reading data from an Excel file, baseline
for performing baseline correction, and writexl
for exporting the data to a new Excel file. The code then reads the data from an Excel file, converts it to a matrix, and performs baseline correction on the data using the fillPeaks
method with specified parameters (lambda = 0.1875, hwi = 400, it = 5, int = 12.5). The corrected data is then converted back to a data frame, and the first column of the original data is added as the first column in the corrected data frame. The final corrected data frame is then exported to a new Excel file.
How to Run the Code
To run this R script, you will need to have R and the required libraries installed on your computer.
- Install the necessary R libraries if you don’t have them already:
readxl
,baseline
,writexl
,tidyr
,reshape2
, anddplyr
. - Save the R script as a .R file.
- Open R and set the working directory to the location of the R script file.
- Load the necessary libraries by running the following lines of code:
scss
library(readxl)
library(baseline)
library(writexl)
library(tidyr)
library(reshape2)
library(dplyr)
- Replace the file path in the following line of code with the file path of your input Excel file:
swift
input_data <- read_excel("C:\\Users\\barbi\\Desktop\\input_data.xlsx")
- Run the script.
- Check the working directory for a newly created Excel file named
baseline_corrected.xlsx
. This file contains the baseline corrected spectra.
Overall Code
References:
- Wickham, Hadley; Bryan, J. Readxl: Read Excel Files. 2019. https://cran.r-project.org/package=readxl.
- Liland, K. H.; Almøy, T.; Mevik, B. Optimal Choice of Baseline Correction for Multivariate Calibration of Spectra. Appl Spectrosc 2010, 64, 1007–1016.
- Ooms, J. writexl: Export Data Frames to Excel “xlsx” Format.
Wickham, H.; Vaughan, D.; Ushey, K. tidyr: Tidy Messy Data. - Wickham, H. Reshaping Data with the Reshape Package. J Stat Softw 2007, 21 (12), 1–20.
- Wickham, H. Dplyr: A Grammar of Data Manipulator. 2021.
- R Core Team. R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing: Vienna, Austria 2021. https://www.r-project.org/.