jeudi 21 janvier 2016

R If or Ifelse statement - Execute code whether dataframe column condition is true or false

I have a data-frame with a temperature column. I want to check if the temperature is above or below 0 degrees Celsius. If the temperature in the column DBT_C is less than or equal to 0, I want first set of code to operate, if it is above 0 then the second set of code should operate. DBT_K is just DBT_C converted from degrees Celsius to Kelvin. I want to avoid calculating values for P_ws for both conditions (above and below 0) so that I don't run unnecessary code as the data-frames may get lengthy. The code and error are below. Thanks!

if (DBT_C <= 0) {
  C1 = -5.6745359e3 #C1 = -1.0214165e4
  C2 = 6.3925247e0 #C2 = -48932428e0
  C3 = -9.6778430e-3 #C3 = -5.3765794e-3
  C4 = 6.2215701e-7 #C4 = 1.920237e-7
  C5 = 2.0747825e-9 #C5 = 3.5575832e-10
  C6 = -9.4840240e-13#C6 = -9.0344688e-14
  C7 = 4.1635019e0 #C7 = 4.1635019e0
  P_ws = (exp(C1/DBT_K + C2 + C3*DBT_K + C4*(DBT_K)^2 + C5*(DBT_K)^3 + C6*(DBT_K)^4 + C7*log(DBT_K)))/1000

} else {
  C8 = -5.8002206e3 # C8 = -1.0440397e4
  C9 = 1.3914993e0 # C9 = -1.1294650e1
  C10 = -4.8640239e-2 # C10 = -2.7022355e-2
  C11 = 4.1764768e-5 # C11 = 1.2890360e-5
  C12 = -1.4452093e-8 # C12 = -2.4780681e-9
  C13 = 6.5459673e0 # C13 = 6.5459673e0
  P_ws = (exp(C8/DBT_K + C9 + C10*DBT_K+ C11*(DBT_K)^2 + C12*(DBT_K)^3 + C13*log(DBT_K)))/1000
}

Warning messages:
1: In if (DBT_C <= 0) { :
  the condition has length > 1 and only the first element will be used
2: In if (DBT_C <= 0) { :
  the condition has length > 1 and only the first element will be used

Aucun commentaire:

Enregistrer un commentaire