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
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
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
wrote:
> ifel
ifelse is vectorized.
On Wed, Dec 13, 2017 at 7: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?
>
> Best,
> Jinsong
>
> _
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
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more
5 matches
Mail list logo