NIR spectra are commonly stored in files with the .SPC extension. Most manufacturers provide software with capabilities to convert their file format into the spc format. Thanks to the hyperSpec package, it is now very easy to load SPC binary files into R. Of course, you need first to install the hyperSpec package:
The challenge here is that, in our ASDspectra folder, we have much more SPC files then we have reference numbers in the reference data. Therefore, we will need to develop a routine to select and open only the SPC files of interest. In simple terms, the following steps will be executed:
Create a data frame containing all SPC file path and reference numbers
Find the SPC file corresponding to the first row of the reference data based on the ASD file number (column ASDref) AND the drying step number (column Step); since the files are stored in a separate folder for each step
Read NIR spectra from this SPC file and from the 5 following based on the ASD file number and average them (this is done because 6 separate SPC files were recorded for each sample)
Store the row of the reference data along with the average spectra in a new data frame
Go to the next row of the reference data and repeat step #2 to #4 until the last row is reached
The R code employed to create the data frame containing all SPC files looks like this:
And the data frame created looks as follows:
Then, to perform the rest of the routine, I used two for loops but there are many other ways to do it: