From eb9d5d2dd726cda8bab5bc085c921fa156b61edf Mon Sep 17 00:00:00 2001 From: Taha Ahmed Date: Sun, 20 Feb 2011 05:43:07 +0100 Subject: [PATCH] Added function ConvertRefPot(). Function takes a potential and its reference electrode, and converts to a specified reference electrode scale, either another or the same. I made it to simplify converting between different reference electrode scales in running text. --- common.R | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/common.R b/common.R index b71ef03..0595261 100755 --- a/common.R +++ b/common.R @@ -3,6 +3,7 @@ # Taha Ahmed, Jan 2011 # CONTENTS +# >>>> ConvertRefPot # >>>> Celsius2Kelvin # >>>> Kelvin2Celsius # >>>> as.radians @@ -11,6 +12,55 @@ +################################################## +################# ConvertRefPot ################## +################################################## +ConvertRefPot <- function(argpotential, argrefscale, valuerefscale) { + # Converts from some reference potential scale into another + # SHE: standard hydrogen electrode scale + # Ag/AgCl: silver silver-chloride electrode scale + # SCE: standard calomel scale + # + ##### Add more reference electrodes here >> + refpotatSHEzero <- c( 0, -0.21, -0.24, 3) + refrownames <- c( "SHE", "Ag/AgCl", "SCE", "Li/Li+") + refcolnames <- c("SHE0", "AgCl0", "SCE0", "Li0") + ##### Add more reference electrodes here << + # + SHE0 <- data.frame(matrix(refpotatSHEzero, ncol=length(refpotatSHEzero), byrow=T)) + refpotmtx <- matrix(NA, length(SHE0), length(SHE0)) + refpotmtx[,1] <- matrix(as.matrix(SHE0), ncol=1, byrow=T) + for (c in 2:length(SHE0)) { + # loop over columns (except the first) + for (r in 1:length(SHE0)) { + # loop over rows + refpotmtx[r, c] <- refpotmtx[r, 1] - refpotmtx[c, 1] + } + } + refpotdf <- as.data.frame(refpotmtx) + names(refpotdf) <- refcolnames + row.names(refpotdf) <- refrownames + ## So far we have made a matrix of all the possible combinations, + ## given the vector refpotatSHEzero. The matrix is not strictly necessary, + ## but it may prove useful later. It does. + # + # Match argrefscale to the refrownames + argmatch <- match(argrefscale, refrownames, nomatch = 0) + # Match valuerefscale to the refrownames + valuematch <- match(valuerefscale, refrownames, nomatch = 0) + # We simply assume that the match was well-behaved + valuepotential <- argpotential + refpotdf[valuematch, argmatch] + # Check that arg and value electrodes are within bounds for a match + if (argmatch == 0 || valuematch == 0) { + # No match + # Perform suitable action + message("Arg out of bounds in call to ConvertRefPot") + valuepotential <- NA + } + return(valuepotential) +} + + ################################################## ############### Celsius2Kelvin ###################