矢量化 IF 语句与 R 中的逻辑 AND 相结合

Vectorized IF statement combined with logic AND in R

这是我希望拥有的代码:

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)

好的,我也可以这样写这段代码,但我希望在你的帮助下我能阻止它。

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)

我也发现了这个问题Vectorized IF statement in R?但是我不能把它转移到我的问题上...

提前感谢您的帮助。

b <- ifelse(a==1 & b<1, 1, b)