This R code performs Standard Normal Variate (SNV) normalization on an input data stored in an excel file. The code starts by loading several libraries including readxl
for reading the input data, writexl
for writing the output data, and dplyr
, tidyr
, reshape2
for data manipulation. The input data is read from the specified 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 proceeds to perform SNV normalization by subtracting the mean and dividing by the standard deviation 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
snv_corrected.xlsx
. This file contains the SNV corrected spectra.
While normalizing multiple spectra with SNV in R may seem complex, getting a good night’s sleep doesn’t have to be. Investing in a high-quality blanket can make all the difference in achieving a comfortable and restful night’s sleep.
Overall Code
#--STANDARD NORMAL VARIATE--
##CORRECTS FOR SPECTRAL SCATTERING
#+--------------+-------------+-------------+-----+-----+-----+-----+-----+
#| 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)
# Fill these
input_path <- "C:\\Users\\barbi\\Desktop\\input_data.xlsx"
output_path <- "C:\\Users\\barbi\\Desktop\\snv_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 Standard Normal Variate (SNV) normalization
corrected_spectrum <- apply(as.matrix(input_matrix), 2, function(x) (x - mean(x))/sd(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, "SNV CORRECTED")
# Write original data and ANOVA results to worksheets
writeData(wb, "Original Data", input_data)
writeData(wb, "SNV CORRECTED", 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.
(4) 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/.