Home > Mobile >  If an A mathematical Arrangement equal Zero Add One
If an A mathematical Arrangement equal Zero Add One

Time:09-27

I have this mathematical arrangement that is true for all integers except 2. I want to sutract 2 from any positive integer(let me stop at 10 for minimum working example's sake). I want ov to be l-2 if l = seq(n - 2) 1

n <- length(1:10) 
l <- seq(n - 2)   1
ov <- l-2

l = 2, 3, 4, 5, 6, 7, 8, 9

ov is true for all my positive integers except 2 that is 0. I want to give a condition that whenever it encounters a ``FALSEsituation it should add1to the equation ofov` as:

ov <- l-2; ov   1

CodePudding user response:

I am not sure if I understand but you can try:

ov[(l - 2) == 0] = ov[(l - 2) == 0]   1
ov

Output:

1 1 2 3 4 5 6 7

CodePudding user response:

You may use pmax.

pmax(ov, 1)
#[1] 1 1 2 3 4 5 6 7

Or an ifelse.

ifelse(ov == 0, ov   1, ov)
  •  Tags:  
  • r
  • Related