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]]
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?
In an if statement, you can use only elements. In your example, news1o and s2o are vectors so there is a warning saying the two vectors have a bigger length than one. If you don't send two messages about the same problem in two minutes, you can see what people answer you... For example, I advised you to use ifelse which works on vectors. Alain On 12-Jul-10 16:02, Raghu 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 ... > > > >-- Alain Guillet Statistician and Computer Scientist SMCS - IMMAQ - Universit? catholique de Louvain Bureau c.316 Voie du Roman Pays, 20 B-1348 Louvain-la-Neuve Belgium tel: +32 10 47 30 50