0

My goal is to determine minimum runoff values ("qmin_2035") that must be sustained in rivers (to ensure that there is enough water for irrigation, industry etc.) according to given relationships for each level of the measured runoff ("q347_2035"), which here is a vector of 283 measurements. So e.g. if q347_2035 is larger than 60 and smaller/equal to 160, the minimum runoff is calculated as a fraction of q347_2035 (see in the code below):

getQMIN <- if(q347_2035 <= 60){
  qMin_2035 = 50
} else if (q347_2035>60 & q347_2035 <= 160) {
  qMin_2035 = 50 + (8/10)*(q347_2035 - 60)
} else if (q347_2035>=160 & q347_2035 <= 500) {
  qMin_2035 = 130 + (4.4/10)*(q347_2035 - 160)
} else if (q347_2035>=500 & q347_2035 <= 2500) {
  qMin_2035 = 280 + (31/100)*(q347_2035 - 500)
} else if (q347_2035>=2500 & q347_2035 < 10000) {
  qMin_2035 = 900 + (21.3/100)*(q347_2035 - 2500)
} else if (q347_2035>=10000 & q347_2035 < 60000) {
  qMin_2035 = 2500 + (150/1000)*(q347_2035 - 10000)
} else {
  qMin_2035 = 10000
}

Now i always get error messages, saying:

Warning messages:
1: In if (q347_2035 <= 60) { :
  the condition has length > 1 and only the first element will be used
2: In if (q347_2035 > 60 & q347_2035 <= 160) { :
  the condition has length > 1 and only the first element will be used
3: In if (q347_2035 >= 160 & q347_2035 <= 500) { :
  the condition has length > 1 and only the first element will be used
4: In if (q347_2035 >= 500 & q347_2035 <= 2500) { :
  the condition has length > 1 and only the first element will be used

can anyone help to resolve the problem? Thanks a lot in advance!

1 Answers1

0

Try using && instead of & in your statements. For further explanation as to why, check out the answer on this post: Boolean operators && and ||.

q347_2035 = 60

getQMIN <- if(q347_2035 <= 60){
qMin_2035 = 50
} else if (q347_2035>60 && q347_2035 <= 160) {
qMin_2035 = 50 + (8/10)*(q347_2035 - 60)
} else if (q347_2035>=160 && q347_2035 <= 500) {
qMin_2035 = 130 + (4.4/10)*(q347_2035 - 160)
} else if (q347_2035>=500 && q347_2035 <= 2500) {
qMin_2035 = 280 + (31/100)*(q347_2035 - 500)
} else if (q347_2035>=2500 && q347_2035 < 10000) {
qMin_2035 = 900 + (21.3/100)*(q347_2035 - 2500)
} else if (q347_2035>=10000 && q347_2035 < 60000) {
qMin_2035 = 2500 + (150/1000)*(q347_2035 - 10000)
} else {
qMin_2035 = 10000
}

getQMIN #Result is 50
Amy
  • 36
  • 3
  • thanks, that works but unfortunately not for the first row: if (q347_2035 <= 60) {, can you help with how to solve this? – greenfields Jan 19 '21 at 16:18
  • It should work with the answer I posted above. Try running all of the code again before you call getQMIN, because if you don't, you'll end up with the wrong number. I'll edit my answer and post the correct code. – Amy Jan 19 '21 at 18:42