In this typical exercise, we will learn how to read multiple files and plot them all together. This is the basic step of data analysis.
Load the tidyverse
and readxl
libraries
Store into flist
the list of all .xls
files in the Data/
folder. Take a look at the list.files()
function for this, and especially at its parameters path
,
pattern
, and full.names
.
Open one of the .xls files with Excel and study the structure of the file. At the top of the file, the first group of 2-column data is the blank measurement (columns A and B, lines 8 through 3408), while the second group under the blank is the actual raw measurement (columns A and B, lines 3413 through 6813). Working on the first file in the list, successively:
blank.df
raw.df
alldata
containing
the columns w
, blank
, raw
, and
transmittance
, the later being the transmittance (in
percent) with respect to the blank measurement (i.e.
transmittance = raw / blank * 100
).The final table should look like this:
## # A tibble: 3,401 × 4
## w blank raw transmittance
## <dbl> <dbl> <dbl> <dbl>
## 1 4000 100 94.2 94.2
## 2 3999 100 94.3 94.3
## 3 3998 100 94.2 94.2
## 4 3997 100 94.2 94.2
## 5 3996 100 94.2 94.2
## 6 3995 100 94.2 94.2
## 7 3994 100 94.2 94.2
## 8 3993 100 94.2 94.2
## 9 3992 100 94.2 94.2
## 10 3991 100 94.2 94.2
## # … with 3,391 more rows
Wrap this procedure into a function readFTIR()
that,
given a file name, returns a tibble with the columns w
,
blank
, raw
, and transmittance
.
This means you just need to do the same thing as before but providing
the file name as a variable, and finish the function by printing the
alldata
tibble – if you don’t print it as the last action
of your function, it won’t return the tibble. Test it on the first file
in the list.
Plot the first file in flist
, and try to make your
plot look like this:
for
loop.
This is the step-by-step procedure:
df
for
loop on all elements of
flist
for
loop, successively:
readFTIR()
function and store it in tempdf
name
to tempdf
containing
the current file name. If needed, trim the path from the name string
using basename()
.tempdf
below df
by binding them
row-wise (look at bind_rows()
).The final table should look like this:
## # A tibble: 34,010 × 5
## w blank raw transmittance name
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 4000 100 94.2 94.2 sample_0_25C.xls
## 2 3999 100 94.3 94.3 sample_0_25C.xls
## 3 3998 100 94.2 94.2 sample_0_25C.xls
## 4 3997 100 94.2 94.2 sample_0_25C.xls
## 5 3996 100 94.2 94.2 sample_0_25C.xls
## 6 3995 100 94.2 94.2 sample_0_25C.xls
## 7 3994 100 94.2 94.2 sample_0_25C.xls
## 8 3993 100 94.2 94.2 sample_0_25C.xls
## 9 3992 100 94.2 94.2 sample_0_25C.xls
## 10 3991 100 94.2 94.2 sample_0_25C.xls
## # … with 34,000 more rows
for
loop
using the tidyverse
. For this, look into the map()
function as
described here.In case you didn’t manage to do the above procedure, just load the
alldata.csv
file in the Data
folder by running
df <- read_csv("Data/alldata.csv")
.
We will now plot all these files together using
ggplot2
.
facet_wrap()
) for
each file.The final plots should look like this:
You see in the file names that there are actually 5 samples with
2 temperatures for each sample. Based on the column name
of
the df
tibble, create the columns sample
and
temperature
that contain numerical values of the
sample number and temperature. Think about using the function gsub()
to replace
(delete) text in a string, the substr(string, start, end)
function to get a portion of a string. You can also take a look at the
separate()
function from the tidyverse
(see here).
Now it is time to make nicer plots:
The final plots should look like this: