jeudi 11 mars 2021

Parallel operation with 'if' condition

I am working on preparing my own functions that will work on pretty big databases. I can see they are slow when there are built in loops so I am trying to avoid them. This one so far I don't know how to avoid:
I want to prepare a vector, which is made out of two other vectors in a way that depends on their components. Here is an example:

v1 <- 1:11
v2 <- 11:1

v3 <- vector()

for (i in 1:11){

       if(v1[i] < v2[i]){

               v3[i] <- v1[i] + v2[i]

       }else if(v1[i] > v2[i]){

               v3[i] <- v1[i] - v2[i]
       }else{

               v3[i] <- NA

       }

}

Can you please explain me how to create v3 without a loop? The function shall eventually work with much more complex if-else statements (ladder and built-in ones).
Thanks!

Aucun commentaire:

Enregistrer un commentaire