I measure many different chemical compounds, each of which has a different calibration curve using one instrument. I would like to apply the correct calibration curve based on the connection name to the raw data to the raw data from the tool. So, I start with a few calibration curves and a raw data data frame:
#generate the calibration curves x <- 1:10 calib.data.1 <- x+runif(10) lm.1 <- lm(calib.data.1~x) calib.data.2 <- 2*x+runif(10) lm.2 <- lm(calib.data.2~x)
Raw data is as follows:
compound <- factor(c("cpd1", "cpd2")) values <- runif(2) raw <- data.frame(compound, values)
It seems that an elegant way to choose the right calibration curve will include ddply or the like. However, I cannot figure out how to do this without writing a function in the following lines:
choose.calib <- function(raw, cpd) if(cpd=="cpd1"){ calib=coef(lm.1)[1]+val*coef(lm.2)[2] }else{ if(cpd=="cpd2"){ calib=coef(lm.2)[1]+val*coef(lm.2)[2] }else{ warning("no calib curve for compound")}} }
Then I would do something like
cal<-ddply(raw, .(compound), choose.calib)
(which does not work anyway due to my inability to understand if-else, but I think I can solve it myself)
Is there a more vector way to do this?
source share