1

Hi I am trying to create a new column called "pCO2" in my data set called "Erie". My dataset currently has ph, alk, and temp values. there is a package called AquaEnv that lets you predict the CO2 value if you have the pH and alkalinity value.

This is the equation to find the values I want in the "pCO2 column"

ae<-aquaenv(S=0,t=0,p=0, SumCO2 = NULL, pH= 7.7,TA=90 )
ae$SumCO2

Is there a way to do this while referencing the corresponding pH and alkalinity values in my Erie dataset?

this was my idea, but it does not give the correct values.

Erie$pCO2<-aquaenv(S=0,t=0,p=0, SumCO2 = NULL, pH=Erie$pH,TA= 
  Erie$ALKALINITY..mg.L.)

Thanks!

user295691
  • 7,108
  • 1
  • 26
  • 35

1 Answers1

1

This is a surprisingly common question, with no real good answer.

Quickest way to get it working is:

df$SumCO2 <- sapply(
  split(Erie, 1:nrow(Erie)),
  function(r) {
    return(aquaenv(S=0,t=0,p=0,SumCO2=NULL, pH=r$pH, TA=r$ALKALINITY..mg.L.)$SumCO2)
  }
)

To walk it through, this splits the Erie data.frame into a set of data.frames (one per row) and for each row invokes this function.

Slightly cleaner is the mapply approach:

mapply(
  function(pH, TA) {
    return(aquaenv(S=0,t=0,p=0,SumCO2=NULL, pH=pH, TA=TA)$SumCO2)
  }, Erie$pH, Erie$ALKALINITY..mg.L
)

Some similar questions with other approaches:

user295691
  • 7,108
  • 1
  • 26
  • 35