-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Gopi Goteti
Sent: Friday, August 24, 2012 6:50 AM
To: r-help at r-project.org
Subject: [R] updating elements of a vector sequentially - is there a
faster way?
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]]