lundi 3 février 2020

If Loop with several conditions

I have the following dataframe:

df1
Name      Ch1      Val1
A          a         x1
B          b         x2
C          a         x3
...

And I want to add another row that gives me a solution on the loop I am trying to get:

for (i in nrow(df))
  if ( (df[i,3]>=-2)==T & (df3[i,3] <=2)==T & df[i,2]=="a"){
    df[i,4]<-TRUE
  }else if ((df[i,3]>2)==T & df[i,2]=="b"){
    df[i,4]<-TRUE
  }else (df[i,4]<-FALSE)

So basically if the value in Val1 is in an interval of -2 and +2 AND Ch1 is "a" it should result in TRUE OR if Val1 is bigger than 2 AND Ch1 is "b" then the result is TRUE

Otherwise it should always be false.

My loop seems to only return the result for the first row the rest is NA. Any idea where the mistake is? Or another way to solve this (even though I actually have a few more ORs)

Thank you!

Aucun commentaire:

Enregistrer un commentaire