Dear all, this is my first post and I?m very new at R, I don?t know if somebody can help with an issue. I?m doing some exercices and I don?t know if for example I can convert this for in an operation with vectors: for (i in 2:n){ if (ES[i-1]==0 && sprd[i-1]>mediaSDP[i-1] && sprd[i]<=mediaSDP[i]) {ES[i] <- -1} else if((ES[i-1]==-1 || ES[i-1]==-15) && sprd[i]>media[i]) {ES[i] <- -15} else if((ES[i-1]==-1 || ES[i-1]==-15) && sprd[i]<=media[i]) {ES[i] <- -17} else if(ES[i-1]==0 && sprd[i-1]<mediaSDN[i-1] && sprd[i]>=mediaSDN[i]) {ES[i] <- 1} else if((ES[i-1]==1 || ES[i-1]==15)&& sprd[i]<media[i]) {ES[i] <- 15} else if((ES[i-1]==1 || ES[i-1]==15)&& sprd[i]>=media[i]) {ES[i] <- 17} else {ES[i] <-0} } Thanks a lot!! -- View this message in context: http://r.789695.n4.nabble.com/Loops-and-vector-operations-tp4240434p4240434.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt <michael.weylandt@gmail.com>
2011-Dec-28 20:42 UTC
[R] Loops and vector operations
Look at ?ifelse. You'll need to nest them however. If you are feeling a little more ambitious this can be done with ?switch, but for clarity, I'd rather see pre-allocation followed by assignments Something along this pattern: out <- numeric(50) x <- sample(3, 50, TRUE) out[x==3] <- "A" out[x==2] <- "B" out[x==1] <- "C" Not the fastest, but quite clear HTH, Michael On Dec 28, 2011, at 10:55 AM, Mago84 <davidquiasmo at gmail.com> wrote:> Dear all, > this is my first post and I?m very new at R, I don?t know if somebody can > help with an issue. > I?m doing some exercices and I don?t know if for example I can convert this > for in an operation with vectors: > > for (i in 2:n){ > if (ES[i-1]==0 && sprd[i-1]>mediaSDP[i-1] && sprd[i]<=mediaSDP[i]) {ES[i] > <- -1} else > if((ES[i-1]==-1 || ES[i-1]==-15) && sprd[i]>media[i]) {ES[i] <- -15} else > if((ES[i-1]==-1 || ES[i-1]==-15) && sprd[i]<=media[i]) {ES[i] <- -17} else > if(ES[i-1]==0 && sprd[i-1]<mediaSDN[i-1] && sprd[i]>=mediaSDN[i]) > {ES[i] <- 1} else > if((ES[i-1]==1 || ES[i-1]==15)&& sprd[i]<media[i]) {ES[i] <- 15} else > if((ES[i-1]==1 || ES[i-1]==15)&& sprd[i]>=media[i]) {ES[i] <- 17} > else {ES[i] <-0} > } > > Thanks a lot!! > > -- > View this message in context: http://r.789695.n4.nabble.com/Loops-and-vector-operations-tp4240434p4240434.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.
Thanks a lot, it was very helpful, I did something like that: EV <- ifelse(sprd > mediaSDP & sprd_d <= mediaSDP_d, -1, 0) SV <- ifelse(sprd > media & sprd_d <= media_d, -17, 0) EC <- ifelse(sprd < mediaSDN & sprd_d >= mediaSDN_d,1,0) SC <- ifelse(sprd < media & sprd_d >= media_d,17,0) in order to have 4 vectors and work with them. Happy new year and kind regards -- View this message in context: http://r.789695.n4.nabble.com/Loops-and-vector-operations-tp4240434p4246200.html Sent from the R help mailing list archive at Nabble.com.
Apparently Analagous Threads
- R-project: plot 2 zoo objects (price series) that have some date mis-matches
- [PATCH 00/53] drm: Convert to platform remove callback returning void
- [PATCH 00/51] treewide: Switch to __pm_runtime_put_autosuspend()
- [PATCH v2 0/8] drm: make leftover drivers call drm_atomic_helper_shutdown() at the right times
- [RFT PATCH v2 00/12] drm: call drm_atomic_helper_shutdown() at the right times