Re: [R] difference between ifelse and if...else?

2017-12-13 Thread MacQueen, Don
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

Re: [R] difference between ifelse and if...else?

2017-12-13 Thread Duncan Murdoch
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

Re: [R] difference between ifelse and if...else?

2017-12-13 Thread 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 wrote: > ifel

Re: [R] difference between ifelse and if...else?

2017-12-13 Thread jeremiah rounds
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 > > _

[R] difference between ifelse and if...else?

2017-12-13 Thread Jinsong Zhao
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