I am newbie in R and I would like to upgrade my code for processing chromatographic peaks using some kind of loop (for cycle or lapply probably?) but I am not able to make it universal for different number of compounds and concnetrations of calibration solutions.
I will show you example of my code for 4 compounds and 7 calibration solution. First I define name of compounds (comp1:comp4), concentrations of calibration solutions (conc1:conc7), start times of peaks (rts1:rts4) and end times of peaks (rtf1:rtf4)
comp1 = "DHP"; comp2 = "DHNP"; comp3 = "BBP"; comp4 = "DHNUP"
conc1 = 0; conc2 = 0; conc3 = 0; conc4 = 5; conc5 = 10; conc6 = 15; conc7 = 20
rts1 = 12.61; rts2 = 13.79; rts3 = 14.34; rts4 = 14.79
rtf1 = 13.25; rtf2 = 14.24; rtf3 = 14.72; rtf4 = 15.00
Then i upload data (in for of dataframe), where first column is time (a$time) and the rest columns represents signal intensity (a$intensity1:a$intensity7 for each calibration solution)
setwd("C:/Users/fojt/Desktop/R/test2")
library(readr)
library(OrgMassSpecR)
library(grid)
options(scipen=999)
a = read.csv("azo_standard_B.csv", sep = ";", quote = "", header = TRUE, dec = ",")
And now fun begins. I made the code below for each of calibration solution (shown for first two of them), which is obviously ugly and time consuming when modifying for experimental conditions. I use function DrawChromatogram from package OrgMassSpecR, which integrate peak within defined time boundaries, gives peaks area and peak height and plot peaks with integrated peaks (to check integration)
# first calibration solution
df1 = DrawChromatogram(a$time,
a$intensity1,
range = list(start = c(rts1, rts2, rts3, rts4),
stop = c(rtf1, rtf2, rtf3, rtf4)),
color = 1:4,
xlab = "Retention time (min)",
ylab = "Intensity",
main = paste(conc1, "mg/l"), las=3)
# dsecond calibration solution
df2 = DrawChromatogram(a$time,
a$intensity2,
range = list(start = c(rts1, rts2, rts3, rts4),
stop = c(rtf1, rtf2, rtf3, rtf4)),
color = 1:4,
xlab = "Retention time (min)",
ylab = "Intensity",
main = paste(conc2, "mg/l"), las=3)
etc
Then I put all data together for further analysis (I need to transpose rows and colums to have first column with concentrations of all solutions and each other row with peak area and header Area)
library(dplyr)
k = select(df1, peakArea)
l = select(df2, peakArea)
m = select(df3, peakArea)
o = select(df4, peakArea)
p = select(df5, peakArea)
q = select(df6, peakArea)
r = select(df7, peakArea)
d = cbind(k, l, m, o, p, q, r)
library(data.table)
dt = transpose(d)
concdat = data.frame(Conc = c(conc1, conc2, conc3, conc4, conc5, conc6, conc7))
dcal = cbind(concdat, dt)
colnames(dcal) <- c("Conc", "Area", "Area", "Area", "Area")
So to sum up, can you please show me how to make it universal for defined number of concentrations and compounds or if you find some other things how to improve my code, i am humbly asking for your advice.
I have tried to solve this with for cycle or lapply, but I haven't been succesfull. Probably because poor syntax.