I have two vectors: both have possible values of 1,-1, or 0 trend1 <- c(1,1,1,1,1,1,-1,-1,-1,-1,-1,-1) trend2 <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,) i want to create a third vector that is conditional upon these two vectors: if (trend2 == 1 && trend1 == 1) {position <- 1} elseif (trend2 == -1 && trend1== -1) {position <- 1} else {position <- 0} based on this two conditions, the position vector should have a value: position: 1,1,1,1,1,1,0,0,0,0,0,0 Is there a way to do this in R without explicitly going through a for loop? The if condition doesn't work as it only accept one condition (aka no vectors). [[alternative HTML version deleted]]
there are also vectorized logical operators; have a look at the help page ?'&', and try this: trend1 <- c(1,1,1,1,1,1,-1,-1,-1,-1,-1,-1) trend2 <- c(1,1,1,1,1,1,1,1,1,1,1,-1) position <- as.numeric((trend1 == 1 & trend2 == 1) | (trend1 == -1 & trend2 == -1)) position I hope it helps. Best, Dimitris On 2/24/2011 9:41 AM, Kushan Thakkar wrote:> I have two vectors: both have possible values of 1,-1, or 0 > > trend1<- c(1,1,1,1,1,1,-1,-1,-1,-1,-1,-1) > trend2<- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,) > > i want to create a third vector that is conditional upon these two vectors: > > if (trend2 == 1&& trend1 == 1) {position<- 1} > elseif (trend2 == -1&& trend1== -1) {position<- 1} > else {position<- 0} > > based on this two conditions, the position vector should have a value: > > position: 1,1,1,1,1,1,0,0,0,0,0,0 > > Is there a way to do this in R without explicitly going through a for loop? > The if condition doesn't work as it only accept one condition (aka no > vectors). > > [[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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/
Hi, I think you want ifelse(). Assuming that length(trend2)=12 and not 14 as in the data you provided, that should work: position <- ifelse((trend1==1 & trend2==1)|(trend1==-1 & trend2==-1),1,0) #btw, note the single "&" and single "|", not double HTH, Ivan Le 2/24/2011 09:41, Kushan Thakkar a ?crit :> I have two vectors: both have possible values of 1,-1, or 0 > > trend1<- c(1,1,1,1,1,1,-1,-1,-1,-1,-1,-1) > trend2<- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,) > > i want to create a third vector that is conditional upon these two vectors: > > if (trend2 == 1&& trend1 == 1) {position<- 1} > elseif (trend2 == -1&& trend1== -1) {position<- 1} > else {position<- 0} > > based on this two conditions, the position vector should have a value: > > position: 1,1,1,1,1,1,0,0,0,0,0,0 > > Is there a way to do this in R without explicitly going through a for loop? > The if condition doesn't work as it only accept one condition (aka no > vectors). > > [[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. >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php