[R] How to get rid of loop?
Peter Alspach
PAlspach at hortresearch.co.nz
Sun Apr 26 22:50:19 CEST 2009
Tena koe Ken
Would something along the following lines do what you require:
set.seed(1)
x <- runif(100)
y <- rep(NA, length(x))
y[x<0.25] <- -1
y[x>0.75] <- 1
y[-1][y[-length(y)]%in%1 & (x[-1]>=0.25 & x[-1]<0.5)] <- 0
# etc
HTH ....
Peter Alspach
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Ken-JP
> Sent: Saturday, 25 April 2009 12:16 p.m.
> To: r-help at r-project.org
> Subject: [R] How to get rid of loop?
>
>
> set.seed(1)
> x <- runif(100)
>
> # I want to calculate y such that:
> #
> # 1. if x>0.75, y <- 1
> # 2. else if x<0.25, y <- -1
> # 3. else if y_prev==1 && x<0.5, y <- 0
> # 4. else if y_prev==-1 && x>0.5, y <- 0 # 5. else y <-
> y_prev # # 1. and 2. are directly doable without looping.
> #
> # How do I do 3.-5. without looping? The problem is, I need
> to run this algorithm over gigs of data, so I # need to avoid
> looping, if at all possible...
> #
> # - Ken
>
>
>
> --
> View this message in context:
> http://www.nabble.com/How-to-get-rid-of-loop--tp23226779p23226779.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
The contents of this e-mail are confidential and may be subject to legal privilege.
If you are not the intended recipient you must not use, disseminate, distribute or
reproduce all or any part of this e-mail or attachments. If you have received this
e-mail in error, please notify the sender and delete all material pertaining to this
e-mail. Any opinion or views expressed in this e-mail are those of the individual
sender and may not represent those of The New Zealand Institute for Plant and
Food Research Limited.
More information about the R-help
mailing list