jeudi 21 janvier 2021

How to randomly replace a value

I have a vector of a certain length of which I want to randomly replace every 2 by 0 or 1, with a probability of 0.4 (for value=1). I have used this code below. I expected to have a different value (0 or 1) for the different 2 replaced, but I have only 1 or 0 that replace the 2.

vec<-c(rep(2,18),1,0)

ifelse (vec==2,rbinom(1,1,0.40)

here is one output

[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0

and another output

 [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0



Aucun commentaire:

Enregistrer un commentaire