Re: [R] Warning: condition has length > 1

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sat 08 Oct 2005 - 23:25:04 EST

On 10/8/05, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
> ifelse(x >= 1, 0, x^2)
>
> or
>
> (x >= 1) * x^2

Sorry, that should have been (x < 1) * x^2

>
> Also, you might need x > (1 - e) where e is some small number since your
> numbers may not be exact.
>
>
>
> On 10/8/05, nawaf b <talda@hotmail.com> wrote:
> > Hi -
> >
> > I was wondering if anyone came across a problem such as mine! I was trying
> > to create a user-defined function, however, the results from calling the
> > function are unexpected!
> >
> > When passing X parameter as a single value variable (x<-c(3)), everything
> > works fine. However, when passing a parameter as a vector with multiple
> > values (as the case in my code), the 'if condition' is only executed once!
> >
> > Here is my code:
> > > x
> > [1] 0.3 1.0 0.7 22.0
> > > myfunction<-function(x)

> > + {
> > + if (x>=1) 0
> > + else x^2
> > + }
> > > myfunction(x)
> > [1] 0.09 1.00 0.49 484.00
> > Warning message:
> > the condition has length > 1 and only the first element will be used in: if
> > (x >= 1) 0 else x^2
> > >
> >
> > Is there a way to overcome this issue? Can you please explain what
> > modifications to the code I need to accommodate to make it work.

> >
> > Thanks,
> >
> > Nawaf
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Oct 08 23:40:38 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 18:32:15 EST