[R] Faster way to do it??...using apply?

M.Ribeiro mresendeufv at yahoo.com.br
Tue Dec 28 17:43:16 CET 2010


Hi, 
I have a simple task, but I am looking for a clever and fast way to do it:

I have a vector x with 0,1 or 2 and I want to create another vector y with
the same length following the rules:
If the element in x is equal to 0, the element in y is equal to 0
If the element in x is equal to 2, the element in y is equal to 1
If the element in x is equal to 1, the element in y is either 0 or 1 (sample
from c(0,1))

thus the vector
> x
     [,1]
[1,]    0
[2,]    2
[3,]    1
[4,]    2
[5,]    0
[6,]    1
[7,]    2

could produce the vector y (this is one of the possibilities since y|x=1 is
either 0 or 1

> y
     [,1]
[1,]    0
[2,]    1
[3,]    1
[4,]    1
[5,]    0
[6,]    0
[7,]    1


I know how to do this using for loops but I was wondering if you guys could
suggest a better way
Thanks

-- 
View this message in context: http://r.789695.n4.nabble.com/Faster-way-to-do-it-using-apply-tp3166161p3166161.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list