Home > Blockchain >  If-Else Statements with OR in R
If-Else Statements with OR in R

Time:11-11

Is it possible to change an if-else statement to include OR in an R-Script? Like 70% of the time would turn into this, while the remaining 30% would turn into something else. Similar to increasing the probability to change?

if (w0[i, j] == 1) { w1[i, j] <- rL[nNeigh   1]
        } else             { w1[i, j] <- rD[nNeigh   1] }

This is the statement where the result is deterministic, but I want to change it into a probabilistic function.

if (w0[i, j] == 1) { w1[i, j] <- rL[nNeigh   1] || w1[i, j] <- rL[nNeigh]
        } else             { w1[i, j] <- rD[nNeigh   1] || w1[i, j] <- rL[nNeigh] }

I know this is not the right way to even do it, but I'm at a loss

CodePudding user response:

Just a quick idea:

coin <- rbinom(1, 1, 0.3)

if (w0[i, j] == 1) { 
  w1[i, j] <- coin * rL[nNeigh   1]   (1 - coin) * rL[nNeigh]
} else {
  w1[i, j] <- coin * rD[nNeigh   1]   (1 - coin) * rL[nNeigh] 
}

CodePudding user response:

It's not clear what the larger context of the question is. However note that Martin Gal's useful answer can be simplified:

coin <- rbinom(1, 1, 0.3)

if (w0[i, j] == 1) { 
  w1[i, j] <- rL[nNeigh   coin]
} else {
  w1[i, j] <- rD[nNeigh   coin] 
}
  • Related