difference between ifelse and if...else?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

difference between ifelse and if...else?

Jinsong Zhao-2
Hi there,

I don't know why the following codes are return different results.

 > ifelse(3 > 2, 1:3, length(1:3))
[1] 1
 > if (3 > 2) 1:3 else length(1:3)
[1] 1 2 3

Any hints?

Best,
Jinsong

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: difference between ifelse and if...else?

jeremiah rounds-2
ifelse is vectorized.

On Wed, Dec 13, 2017 at 7:31 AM, Jinsong Zhao <[hidden email]> wrote:

> Hi there,
>
> I don't know why the following codes are return different results.
>
> > ifelse(3 > 2, 1:3, length(1:3))
> [1] 1
> > if (3 > 2) 1:3 else length(1:3)
> [1] 1 2 3
>
> Any hints?
>
> Best,
> Jinsong
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: difference between ifelse and if...else?

Eric Berger
ifelse returns the "shape" of the first argument

In your ifelse the shape of "3 > 2" is a vector of length one, so it will
return a vector length one.

Avoid "ifelse" until you are very comfortable with it. It can often burn
you.




On Wed, Dec 13, 2017 at 5:33 PM, jeremiah rounds <[hidden email]>
wrote:

> ifelse is vectorized.
>
> On Wed, Dec 13, 2017 at 7:31 AM, Jinsong Zhao <[hidden email]> wrote:
>
> > Hi there,
> >
> > I don't know why the following codes are return different results.
> >
> > > ifelse(3 > 2, 1:3, length(1:3))
> > [1] 1
> > > if (3 > 2) 1:3 else length(1:3)
> > [1] 1 2 3
> >
> > Any hints?
> >
> > Best,
> > Jinsong
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posti
> > ng-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: difference between ifelse and if...else?

Duncan Murdoch-2
In reply to this post by Jinsong Zhao-2
On 13/12/2017 10:31 AM, Jinsong Zhao wrote:

> Hi there,
>
> I don't know why the following codes are return different results.
>
>   > ifelse(3 > 2, 1:3, length(1:3))
> [1] 1
>   > if (3 > 2) 1:3 else length(1:3)
> [1] 1 2 3
>
> Any hints?

The documentation in the help page ?ifelse and ?"if" explains it pretty
clearly.  Think of ifelse() as a function with vector inputs and a
vector output, and if() as a flow control construction.

Duncan Murdoch

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: difference between ifelse and if...else?

MacQueen, Don
In reply to this post by Jinsong Zhao-2
Because ifelse is not intended to be an alternative to if ... else. They exist for different purposes.

(besides the other replies, a careful reading of their help pages, and trying the examples, should explain the different purposes).

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

On 12/13/17, 7:31 AM, "R-help on behalf of Jinsong Zhao" <[hidden email] on behalf of [hidden email]> wrote:

    Hi there,
   
    I don't know why the following codes are return different results.
   
     > ifelse(3 > 2, 1:3, length(1:3))
    [1] 1
     > if (3 > 2) 1:3 else length(1:3)
    [1] 1 2 3
   
    Any hints?
   
    Best,
    Jinsong
   
    ______________________________________________
    [hidden email] mailing list -- To UNSUBSCRIBE and more, see
    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.
   

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.