The code is an R script that performs max-min normalization on multiple spectra stored in an excel file. The code starts by loading the necessary libraries, including readxl, writexl, tidyr, reshape2, and dplyr. The input data is then read from the excel file into a data frame, and then converted into a matrix. The code checks for missing and infinite values in the input matrix and then performs max-min normalization by subtracting the minimum value and dividing by the range for each column in the matrix. The corrected data is then converted into a data frame, rearranged, and the first column is renamed to “wave_number”. Finally, the corrected data is exported as 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
,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(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
max_min_ns_corrected.xlsx
. This file contains the MAX-MIN normalized corrected spectra.
Overall Code
## MAX-MIN NORMALIZATION FOR MULTIPLE SPECTRA
##NORMALIZES SPECTRA TO INTENSITY GOING FROM 0 TO 1
#+--------------+-------------+-------------+-----+-----+-----+-----+-----+
#| Wave Number | Sample 01 | Sample 02 | ... | ... | ... | ... | ... |
#+--------------+-------------+-------------+-----+-----+-----+-----+-----+
#| 788 | Value | Value | ... | ... | ... | ... | ... |
#| 792 | Value | Value | ... | ... | ... | ... | ... |
#| 796 | Value | Value | ... | ... | ... | ... | ... |
#| 800 | ... | ... | ... | ... | ... | ... | ... |
#+--------------+-------------+-------------+-----+-----+-----+-----+-----+
# Load necessary libraries
library(readxl)
library(writexl)
library(tidyr)
library(reshape2)
library(dplyr)
library(openxlsx)
# Fill these
input_path <- "C:\\Users\\barbi\\Desktop\\input_data.xlsx"
output_path <- "C:\\Users\\barbi\\Desktop\\ns-max-min_output.xlsx"
# Read data from excel file into a data frame
input_data <- read_excel(input_path)
#########################################################################################
#PART 01: TRANSFORMING SPECTRAL DATA
#########################################################################################
# Changes the data from a data frame to a matrix
input_matrix <- as.matrix(input_data)
# Check for missing and infinite values
sum(is.na(input_matrix))
sum(is.infinite(input_matrix))
# Perform Maximum-Minimium normalization from 0 to 1
corrected_spectrum <- apply(as.matrix(input_matrix), 2, function(x) (x - min(x))/(max(x) - min(x)))
# Changes the format from a long table to a wide table
corrected_df <- data.frame(wave_number = seq_len(nrow(input_data)), as.data.frame(corrected_spectrum))
corrected_df <- corrected_df[,-1] #eliminates first column
# Eliminate the first column in corrected_df
corrected_df <- corrected_df %>% select(-1)
# Add the first column in input_data as the new first column in corrected_df
corrected_df <- cbind(input_data[,1], corrected_df)
# Change the name of the first column to "wave_number"
names(corrected_df)[1] <- "wave_number"
# Converting "corrected_df" to a data frame again
corrected_df <- data.frame(corrected_df)
#########################################################################################
#PART 02: SAVING RESULTS
#########################################################################################
# Create a new workbook and add worksheets
wb <- createWorkbook()
addWorksheet(wb, "Original Data")
addWorksheet(wb, "NS MAX-MIN")
# Write original data and ANOVA results to worksheets
writeData(wb, "Original Data", input_data)
writeData(wb, "NS MAX-MIN", corrected_df)
# Save the workbook
saveWorkbook(wb, output_path)
References:
- Wickham, Hadley; Bryan, J. Readxl: Read Excel Files. 2019. https://cran.r-project.org/package=readxl.
- 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/.