I'd like to implement element-wise control flow. An example:
v1 = as.vector(c(1,2,3))
v2 = as.vector(c(3,2,1))
v3 = as.vector(c(0,0,1))
for (i in 1:len(c1)) {
if (c1[i]>=2 && c2[i]<=2) {
v3[i]=1
}
}
# end result: v3 = [0,1,1]
The preferred vector form (which does not work and not efficient):
if (c1 >=2 & c2 <= 2) {
v3 = 1
}
Note that
v3 = c1 >=2 & c2 <= 2 # end result v3=[0,1,0]
does not work because v3 is not supposed to change if the condition is FALSE.
What kind of vector syntax can I use to avoid the for loop? Note that if c1[i] is FALSE, c2[i] will not be examined at all.