How about -- d is a vector as you described it, then define d1 <- d[1:(length(d)-1)] d2 <- d[2:length(d)] d3 <- d1!=0 & d2!=0 & (d1-d2)!=0 I think d3 is the final vector you wanted. I didn't test it, so I'm not sure my logic is right, but I think this is definitely a path you can take... -----Original Message----- From: owner-r-help at stat.math.ethz.ch [mailto:owner-r-help at stat.math.ethz.ch]On Behalf Of Sven Garbade Sent: Wednesday, September 18, 2002 9:35 AM To: r-help at stat.math.ethz.ch Subject: [R] comparing elements in a vector Hi all, there is a vector d which contains three different valus: 0, 1 or 2. I wrote a loop which gives me T, when the values in d changed from 1 to 2 or from 2 to 1, otherwise F: for(i in 1:(length(d)-1)) { if(d[i] != d[i+1] & d[i] != 0 & d[i+1] != 0) change[i+1] <- T else change[i+1] <- F } This works, but I'm wondering if there is already a R function or an easier way to avoid the for-loop. Thanks, Sven -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
If you are only interested in the 2-1 and 1-2 sequence, then the product of the successive elements must be 2 (i.e., 1*2 = 2*1 = 2). So try this: change <- d[-length(d)] * d[-1] == 2 Andy> -----Original Message----- > From: Sven Garbade [mailto:garbade at psy.uni-muenchen.de] > Sent: Wednesday, September 18, 2002 9:35 AM > To: r-help at stat.math.ethz.ch > Subject: [R] comparing elements in a vector > > > Hi all, > > there is a vector d which contains three different valus: 0, 1 or 2. I > wrote a loop which gives me T, when the values in d changed from 1 to > 2 or from 2 to 1, otherwise F: > > for(i in 1:(length(d)-1)) { > if(d[i] != d[i+1] & d[i] != 0 & d[i+1] != 0) > change[i+1] <- T > else > change[i+1] <- F > } > > This works, but I'm wondering if there is already a R function or an > easier way to avoid the for-loop. > > Thanks, Sven > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > -.-.-.-.-.-.-.-.- > r-help mailing list -- Read > http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: > r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. > _._._._._._._._._ >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential, proprietary copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please immediately return this by e-mail and then delete it. ============================================================================= -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi all, there is a vector d which contains three different valus: 0, 1 or 2. I wrote a loop which gives me T, when the values in d changed from 1 to 2 or from 2 to 1, otherwise F: for(i in 1:(length(d)-1)) { if(d[i] != d[i+1] & d[i] != 0 & d[i+1] != 0) change[i+1] <- T else change[i+1] <- F } This works, but I'm wondering if there is already a R function or an easier way to avoid the for-loop. Thanks, Sven -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._