On 02/24/2011 08:37 PM, Dimitris Rizopoulos wrote:
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

How about (trend1 * trend2 + 1)/2

Jim


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).

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

Reply via email to