[R] a small puzzle?

jim holtman jholtman at gmail.com
Mon Jul 12 18:13:58 CEST 2010


You probably want to use "ifelse"

s <- ifelse(news1o>s2o, 1, -1)

'if' only handle a single logical expression.


On Mon, Jul 12, 2010 at 10:02 AM, Raghu <r.raghuraman at gmail.com> wrote:
> I know the following may sound too basic but I thought the mailing list is
> for the benefit of all levels of people. I ran a simple if statement on two
> numeric vectors (news1o and s2o) which are of equal length. I have done an
> str on both of them for your kind perusal below. I am trying to compare the
> numbers in both and initiate a new vector s as 1 or 0 depending on if the
> elements in the arrays are greater or lesser than each other. When I do a
> simple s=(news1o>s2o) I get the values of S as a string of TRUEs and FALSEs
> but when I try to override using the if statements this cribs. I get only
> one element in s and that is a puzzle. Any ideas on this please? Many
> thanks.
>
>
> if(news1o>s2o)(s<-1) else
> + (s<--1)
> [1] -1
> Warning message:
> In if (news1o > s2o) (s <- 1) else (s <- -1) :
>  the condition has length > 1 and only the first element will be used
>> s
> [1] -1
>> length(s)
> [1] 1
>  str(news1o)
>  num [1:3588] 891 890 890 888 886 ...
>> str(s2o)
>  num [1:3588] 895 892 890 888 885 ...
>
>
>
>
> --
> 'Raghu'
>
>        [[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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list