mardi 29 mars 2016

Replace values in a vector with a probability R

I don't know how to replace values in a vector with a certain probability. For example:

set.seed(49)
sample(c(0,1), nb_id, prob = c(opposite_prob_sur,prob_survival), replace = TRUE)

Gives something like: [1] 1 1 1 0 1 1 1 1 0 0

Is it possible with this vector to change 1s with 0s with a certain probability, lets say 40% ?

To go a little in the details, I'm generating a dataset with a certain probability of getting 1s vertically. But, I want to control for a probability horizontally at the same time (organisms that are entering in the population)

ch = data.frame(id = 1:10)
for (yr in 1:nb_year) {
  recapture = sample(c(0,1), nb_id, prob = c(opposite_prob_sur,prob_survival), replace = TRUE)
  ch[,yr] = recapture
  colnames_vect[yr] = paste("year", yr, sep = "")
  }
colnames(ch) = colnames_vect
ch




Aucun commentaire:

Enregistrer un commentaire