0

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.

tueftla
  • 369
  • 1
  • 3
  • 16

1 Answers1

1
b <- ifelse(a==1 & b<1, 1, b)
user2332849
  • 1,421
  • 1
  • 9
  • 12