Skip to content

Applying function with separate dataframe (calibration file) supplying some inputs

3 messages · Nathan Miller, Joshua Wiley

#
Hi Nathan,

I honestly do not think that anything else will be much better than
merging the two datasets.  If the datasets are not merged, you
essentially have to apply your optode function to each vial, store the
results, then combine them all together.  This is innefficient.
Merging the datasets may be innefficient in a way, but once it is
done, your function can be applied to the entire dataset in one step.
If you have big data and the merge is slow, take a look at the
data.table package.  I have recently (not that it was bad before, I
just never really knew how much it could do) been quite impressed with
it.  As a whole other note, your optode function was quite difficult
to read, and I highly doubt you can confidently look at the code and
ensure there is not a typo, missed operator, etc. somewhere in that
block of formula code.  I attempted to clean it up some, though
perhaps not with 100% success.

#######################################
optode2 <- function(cal0, T0, cal100, T100, phase, temp) {
  dr <- pi/180
  f1 <- 0.801
  deltaPsiK <- (-0.08)
  deltaKsvK <- 0.000383
  m <- 22.9
  tan0T100 <- tan(((cal0 + deltaPsiK * (T100 - T0))) * dr)
  tan0Tm <- tan((cal0 + (deltaPsiK * (temp - T0))) * dr)
  tan100T100 <- tan(cal100 * dr)
  tanmTm <- tan(phase * dr)
  A <- tan100T100 / tan0T100 / m * 100^2

  B <- tan100T100 / tan0T100 * 100 + tan100T100 / tan0T100 / m *100 -
f1 / m * 100 - 100 + f1 * 100
  C <- tan100T100 / tan0T100 - 1
  KsvT100 <- (- B + (sqrt(B^2 - 4 * A * C))) / (2 * A)
  KsvTm <- KsvT100 + (deltaKsvK * (temp - T100))
  a <- tanmTm / tan0Tm / m * KsvTm^2
  b <- tanmTm / tan0Tm * KsvTm + tanmTm / tan0Tm / m * KsvTm - f1 / m
* KsvTm - KsvTm + f1 * KsvTm
  c <- tanmTm / tan0Tm - 1
  tot <- tanmTm / tan0T100
  big <- tot * KsvTm + tanmTm / tan0T100 / m * KsvTm - f1 / m * KsvTm
- KsvTm + f1 * KsvTm

  saturation <- (-big + (sqrt((big)^2-4 * (tanmTm / tan0T100 / m *
KsvTm^2) * (tot - 1)))) / (2 * (tot / m * KsvTm^2))
  return(saturation)
}

## Read in your example data
d1 <- read.table(textConnection("
vial cal0    T0  cal100  T100
1      61      18    28       18
2      60.8    18    27.1    18
3     60.2    18    28.3     18
4     59.8    18     27.2     18"), header = TRUE, stringsAsFactors = FALSE)
d2 <- read.table(textConnection("
vial   phase    temp   time
1       31            17.5    10
1       31.5          17.4    20
1       32.8          17.5    30
2      29.0           17.5     10
2      29.7           17.5     20
2      30.9           17.5     30
3      27.1           17.4     10
3      27.6           17.4     20
3      28.1           17.5     30
4      31.0           17.6     10
4      33.3           17.6     20
4     35.6            17.6     30"), header = TRUE, stringsAsFactors = FALSE)
closeAllConnections()

dat <- merge(d1, d2, by = "vial")
## optode wrapper
f <- function(d) optode2(d$cal0, d$T0, d$cal100, d$T100, d$phase, d$temp)

dat$oxygen <- f(dat)

dat
#######################################


Cheers,

Josh
On Wed, Oct 19, 2011 at 8:38 PM, Nathan Miller <natemiller77 at gmail.com> wrote: