I would like to know whether there is a faster way to do the below
operation (updating vec1).

My objective is to update the elements of a vector (vec1), where a
particular element i is dependent on the previous one. I need to do this on
vectors that are 1 million or longer and need to repeat that process
several hundred times. The for loop works but is slow. If there is a faster
way, please let me know.

probs <- c(.1, .3, .2, .4, .7, .9, .3, .4, .5, .6)
p10 <- 0.6
p00 <- 0.4
vec1 <- rep(0, 10)
for (i in 2:10) {
  vec1[i] <- ifelse(vec1[i-1] == 0,
                    ifelse(probs[i]<p10, 0, 1),
                    ifelse(probs[i]<p00, 0, 1))
}

Thanks
GG

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to