Jens,
This is very helpful. Thanks a lot.
Qian
On Wed, 27 Feb 2008 08:13:18 -0500
"Jens Hainmueller" <jhainmueller at gmail.com> wrote:
Qian,
range. Why moving "out" out
of the ifelse function would fix the problem is beyond me.
This is a subtle point. Consider the two statements:
# 1
ifelse(x>=1, out <- x^2, out <- -x)
# 2
out <- ifelse(x>=1,x^2,-x)
Now 1 will lead to unanticipated behavior if the condition x>1 is true for
some elements of x but false for others. As long as any elements I true,
"out" will be defined by only the "yes" condition x^2. The reasons is
that R
out put R in a dilemma, it wants to evaluates both x^2 or -x but the output
is called "out" in both cases so out should be subsequently overwritten. In
this case R decides to evaluate all elements of x with only one condition,
the yes condition.
Expression 2 is not affected by this because the out object is defined as
the output of the whole ifelse() command, so R does what you want it to do,
evaluate elements by elements and puts it all together in the out vector
object at the end.
Notice that 1 will still work as you think if the condition x>1 is either
true or false for ALL elements of x, because then only one of the out
objects will be defined.
Always use 2, when in doubt.
Hope this helps.
Jens
For no reason I know of, it works. It seems that
in the previous case,
R somehow always treats
x>=(-pi/2) & x<=(pi/2) as false if any x in a vector falls out of this
> -----Original Message-----
> From: gov2001-l-bounces at
lists.fas.harvard.edu [mailto:gov2001-l-
> bounces at
lists.fas.harvard.edu] On Behalf Of Qian Guo
> Sent: Wednesday, February 27, 2008 7:12 AM
> To: gov2001-l at
lists.fas.harvard.edu
> Subject: Re: [gov2001-l] Qu2
>
> The same thing happened to me when I wrote my function as:
> g <- function(x){
> ifelse(x>=(-pi/2) & x<=(pi/2), out <- 10*sin(x)*cos(x), out <-
> sin(x)*cos(x))
> out
> }
>
> As you said, it worked well for individual values - in fact, it worked
> fine for the optimization
> as well, but not for the graph.
>
> Then I changed my program to
> g <- function(x){
> out <- ifelse(x>=(-pi/2) & x<=(pi/2), 10*sin(x)*cos(x),
> sin(x)*cos(x))
> out
> }
>
For no reason I know of, it works. It seems that
in the previous case,
R somehow always treats
x>=(-pi/2) & x<=(pi/2) as false if any x in a vector falls out of this
range. Why moving "out" out
of the ifelse function would fix the problem is beyond me.
>
> Qian
>
>
>
> On Wed, 27 Feb 2008 07:36:57 +0000
> Jeremy Hodgen <jeremy.hodgen at kcl.ac.uk> wrote:
> > Can anyone help?
> >
> > I'm trying to define the function for qu2
> >
> > This works fine for individual values of x:
> >
> > g <- function(x){
> > if ((x < pi/2) & (x > -1*pi/2)) (y <- 10*sin(x)*cos(x)) else (y
> <- sin(x)*cos(x))
> > return(y)
> > }
> > But when I input a vector (in order to draw the graph) like this:
> >
> > ruler <- seq(-2*pi, 2*pi, by=.01)
> > g(ruler)
> >
> > I get this error message:
> >> Warning message:
> >> In if ((x < pi/2) & (x > -1 * pi/2)) (y <- 10 * sin(x) *
cos(x))
> >> else (y <- sin(x) * :
> >> the condition has length > 1 and only the first element will be
> used
> >>
> >
> >
> > It seems I've got too many conditions for 'if' when the input is
a
> vector. When I draw the
> >graph of g(ruler)~ruler, I get sinxcosx on the domain [-2pi, 2pi].
> Any ideas on what I could do
> >differently?
> >
> > Thanks
> >
> > Jeremy
> >
> >
> >
> > Dr Jeremy Hodgen
> > Senior Lecturer in Mathematics Education
> > King's College London
> > Department of Education and Professional Studies
> >Franklin-Wilkins Building
> > Waterloo Bridge Wing
> > 150 Stamford Street
> > London SE1 9NH
> >
> > Tel: 020 7848 3102
> >Fax: 020 7848 3182
> > E-mail: jeremy.hodgen at kcl.ac.uk
> _______________________________________________
> gov2001-l mailing list
> gov2001-l at
lists.fas.harvard.edu
>
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l
_______________________________________________
gov2001-l mailing list
gov2001-l at
lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l