mardi 17 mars 2020

Vectorized IF statement combined with logic AND in R

This is my code I wish to have:

a=1
b=c(2,1.5,0.7)

if (a==1 & b<1) {

     b=1   # but here's the problem, that only the first value of the vector b is considered

}  # end if loop

print(b)

Okay, I could also write this code like this but I hope I can prevent it with your help.

a=1
b=c(2,1.5,0.7)

if (a==1) {

     for (i in 1:length(b)) {

          if (b[i]<1) {

               b[i]=1  

          }  # end if loop

     }  # end for loop 

}  # end if loop

print(b)

I also have found this question Vectorized IF statement in R? but I cannot transfer it to my issue...

Thanks for your help in advance.

Aucun commentaire:

Enregistrer un commentaire