R365: Day 44 – Cumulative Index with Upper and Lower Boundaries

This topic sounds SUPER specific, when I was writing that title I was like “no one is going to read this, oh well.”

BUT! What this is useful for is setting up an index where you know that the index should not go above or below a certain point.

Yeah…thats pretty specific…

BUT it is useful in plant pathology for calculating the Gubler-Thomas (GT) powdery mildew risk index. I need to program the index for work.

Here is some example script for how to set upper and lower thresholds using iflese() statements

#set up a vector with data
a=rep(1:10)
#set up a function, name it whatever
GTA=function(a){
 #have one of your functions do what you want;
 #this function checks if a is divisible by 3, if yes, sqrt(a), if no, (a)^4
 b=ifelse(a%%3>0,sqrt(a),a^(4))
 #this checks if the value is over 10
 c=ifelse(b>10,10,b)
 #this checks if the value is under -10
 d=ifelse(c<(-10),-10,c)
 print(d)
}
#run the function
q=GTA(a)
#...and plot it out
plot(q)
lines(q)

Then see the beautiful plot:

Rplot14

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s