vendredi 1 mai 2020

Can ifelse be used effectively with outputs that have no return?

Consider the following example code:

a<-0
b<-0
plot(0,0)
ranVec<-runif(100)
f1<-function()#ran 40% of the time
  {
    b<-b+1
    points(b,4)
  }
f2<-function()#ran 60% of the time
  {
    b<-b-1
    points(b,4)
  }

I'm currently dealing with something very similar and my next line of code is effectively:

for(i in 1:100)
{
   if(ranVec[i]<=0.4) f1
   else f2
}

But would it have been possible to achieve this previous block's outcome with ifelse? For example, what I really wanted to write was:

ifelse(rawResults<=0.4, f1, f2)

But this throws an error presumably concerning the length of the output.

Aucun commentaire:

Enregistrer un commentaire