[R] updating elements of a vector sequentially - is there a faster way?

PIKAL Petr petr.pikal at precheza.cz
Fri Aug 24 10:43:49 CEST 2012


Hi

Well, I am not sure if this is what you want but same result can be achieved by

vec1 <- (probs>=p00)*(probs>=p10)

Petr


> -----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]]
> 
> ______________________________________________
> R-help at 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.




More information about the R-help mailing list