This code is a script written in the R programming language that processes a series of .txt files in a specified folder, removes the first row from each file, and combines the resulting data into a single .csv file. The combined data is then rearranged so that each row contains the intensity values for a single wave number and each column represents a different sample. The final resulting .csv file contains the wave numbers and intensity values for all of the samples in the specified folder, with each sample’s data in a separate column. The script makes use of several libraries, including tidyverse, fs, dplyr, and stringr, to perform various tasks such as reading and writing files, manipulating data frames, and modifying strings.
How to Run the Code
To run this R script, you will need to have R and the required libraries installed on your computer.
- Open the R console.
- Load the required libraries by entering the following lines of code:
library(tidyverse)
library(fs)
library(dplyr)
library(stringr)
- Modify the value of the
folder_path
variable to the file path of the folder containing the .txt files you want to process. For example:
folder_path <- "C:\\My Documents\\Raman Data\\TEP"
- Modify the value of the
output_file
variable to the file path where you want to save the resulting .csv file. For example:
output_file <- "C:\\My Documents\\Raman Data\\tep-638nm-XploRA-Raman.csv"
- Run the script by entering the following command:
source("path/to/script.R")
- Wait for the script to complete. The resulting .csv file will be saved to the file path specified in the
output_file
variable.
Overall Code
#MERGE MULTIPLE *.txt FILES IN A FOLDER INTO ONE *.csv and *.xlsx FILE
#Imports the libraries needed to run the script.
library(tidyverse)
library(fs)
library(dplyr)
library(stringr)
#FILL THESE
folder_path <- "C:\\Users\\barbi\\Desktop\\Water (+Acetone)"
output_file <- "C:\\Users\\barbi\\Desktop\\water-acetone.csv"
#Creates one-column table with the path of every file per rows in the specified folder named "all_of_them".
all_of_them <- fs::dir_ls(folder_path)
file_names <- list.files(folder_path)
#Creates empty data frame with two columns: Wave Number and Intensity.
#write.table(data.frame('Wave Number','Intensity'), file = output_file, sep = ",",
#append = TRUE, quote = FALSE,
#col.names = FALSE, row.names = FALSE)
#Loop that reads every file in the folder ands saves the data in my_content.
for (i in seq_along(file_names)) {
my_content <- read.table(
file = all_of_them[[i]], sep="\t", header=TRUE)
#Creates data frame for my_content.
df <- data.frame(my_content)
#Removes rows 1 to 1 for each *.txt file.
header_df <- file_names[i]
new_data <- df[-c(1:1),]
new_df <- data.frame(new_data)
new_df <- new_df%>%dplyr::mutate(sample=header_df)
#Writes table with the data from all the *.txt files and
#exports it in the output_file path.
write.table(new_df, file = output_file, sep = ",",
append = TRUE, quote = FALSE,
col.names = FALSE, row.names = FALSE)
}
#Export the created *.txt
created_csv <- read_csv(output_file)
#Creates a data frame for created_csv
collapsed_csv <- data.frame(created_csv)
#Creates columns "wave_number", "intensity", and "sample_name"
colnames(collapsed_csv) <- c("wave_number","intensity","sample_name")
#Removes characters in the sample_name values
collapsed_csv <- collapsed_csv %>% mutate_at("sample_name", str_replace, ".txt", "")
#Converts the collapsed_csv data frame from long to wide
collapsed_csv <- pivot_wider(collapsed_csv, names_from = sample_name, values_from = intensity)
#Exports the collapsed_csv data frame in an *.csv file
write.csv(collapsed_csv, output_file, row.names = FALSE)
References:
- Wickham, Hadley; Averick, Mara; Bryan, Jennifer; Chang, Wins; D’Agostino McGowan, Lucy; Francois, Romain; Grolemund, Garrett; Hayes, Alex; Hendry, Lionel; Hester, Jim; Kuhn, Max; Lin Pedersen, Thomas; Miller, Evan, Milton Bache, Stephan; Muller, Kirill; O, H. Welcome to the {tidyverse}. The Journal of Open Source Software 2016, 4 (43), 1686. https://doi.org/10.21105/joss.01686.
- Hester, Jim; Wickham, Hadley; Csardi, G. Fs: Cross-Platform File System Operations Based on “Libuv.” 2021. https://cran.r-project.org/package=fs.
- Wickham, H. Dplyr: A Grammar of Data Manipulator. 2021.
- Wickham, H. Stringr: Simple, Consistent Wrappers for Common String Operations. 2019. https://cran.r-project.org/package=stringr.
- R Core Team. R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing: Vienna, Austria 2021. https://www.r-project.org/.