[R] Logical vector question

Joshua Wiley jwiley.psych at gmail.com
Wed Jun 9 00:42:58 CEST 2010


Hello,

This should work

v1 <- runif(20, min=0, max=1)
v2 <- v1 <.3 | v1 >.7 #you just need one | not two

HTH,

Josh

On Tue, Jun 8, 2010 at 3:38 PM, Worik R <worikr at gmail.com> wrote:
> If I create a vector thusly
>
>> v1 <- runif(20, min=0, max=1)
>> v1
>  [1] 0.9754443 0.6306228 0.3238158 0.3175769 0.6791534 0.6956507 0.3840803
>  [8] 0.1421328 0.8592398 0.4388306 0.9472040 0.4727435 0.5645302 0.7391616
> [15] 0.6116199 0.2727754 0.2657867 0.5261744 0.8764804 0.2032126
>
> And I want to create a logical vector the same length that is true if v1<.3
> or v1 > .7  how do I do it without a loop?
>
>> v1>.7
>  [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE
> [13] FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE
>> v1<.3
>  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
> [13] FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE
>
> OK so far but I try...
>> v1<.3 || v1>.7
> [1] TRUE
>>
>
> Not a vector.
>
> I have tried a lot of combinations culminating in...
>
> ifelse(((v1<.3) || (v1>.7)), TRUE, FALSE)
> [1] TRUE
>>
>
> What can I do?
>
> cheers
> Worik
>
>
>
>> v1
>  [1] 1 1 1 0 0 0 0 0 0 1
>> v2
>  [1] 1 1 1 0 1 0 0 0 1 1
>
> The
>
>        [[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.
>



-- 
Joshua Wiley
Senior in Psychology
University of California, Riverside
http://www.joshuawiley.com/



More information about the R-help mailing list