r - air quality index calculation -


if have statement in excel like:

=if(and(q1>=0,q1<=12), 50/12*(q1-0)+0, if(and(q1>=12.1,q1<=35.4),(100-51)/(35.4-12.1)*(q1-12.1)+51, if(and(q1>=35.5, q1<=55.4), (150-101)/(55.4-35.5)*(q1-35.5)+101, if(and(q1>=55.5,q1<=150.4), (200-151)/(150.4-55.5)*(q1-55.5)+151,"na" )))) 

"q1" cell in column q row 1. question is, how can translate statement excel r? guess should use multiple if else statements in r, not sure how organize them...

thank you!

if in excel can ifelse in r. parameters same - @ file typing ?ifelse and in excel translates & between statements in r - see here

#assume data called q result = ifelse((q>=0 & q<=12), 50/12*(q-0)+0, ifelse((q>=12.1 & q<=35.4),(100-51)/(35.4-12.1)*(q-12.1)+51, ifelse((q>=35.5 & q<=55.4), (150-101)/(55.4-35.5)*(q-35.5)+101, ifelse((q>=55.5 & q<=150.4), (200-151)/(150.4-55.5)*(q-55.5)+151,"na" )))) 

testing this, have:

q = c(1,2,3,100,150,60)

> result [1] "4.16666666666667" "8.33333333333333" "12.5"             "173.976817702845" "199.793466807165" "153.323498419389" 

hope helps


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -