[R] need help in if else condition

Richard O'Keefe r@oknz @end|ng |rom gm@||@com
Sun Jul 14 23:55:25 CEST 2019


Four-core AMD E2-7110 running Ubuntu 18.04 LTS.
The R version is the latest in the repository:
r-base/bionic,bionic,now 3.4.4-1ubuntu1 all [installed]

Why not 3.6?  Because when i followed the installation instructions, adding

deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/

to /etc/apt/sources.list, sudo apt update reported

W: GPG error: https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/
InRelease: The following signatures couldn't be verified because the public
key is not available: NO_PUBKEY 51716619E084DAB9
E: The repository 'https://cloud.r-project.org/bin/linux/ubuntu
bionic-cran35/ InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore
disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration
details.

I just repeated the test.
> x <- runif(1000000)
> y <- runif(1000000)
> system.time(ifelse(x<y,x,y))
   user  system elapsed
  0.359   0.043   0.404
> system.time(pmin(x,y))
   user  system elapsed
  0.015   0.008   0.023
> system.time({r<-numeric(1000000);ix <- x < y; r[ix]<-x[ix];
r[!ix]<-y[!ix]; r})
   user  system elapsed
  0.077   0.028   0.105

On Mon, 15 Jul 2019 at 08:00, peter dalgaard <pdalgd using gmail.com> wrote:
>
> Er, what version is this? I have (on a late 2010 MB Air!)
>
> > system.time(ifelse(x < y, x, y))
>    user  system elapsed
>   0.072   0.012   0.085
>
> and even
>
> > system.time({r<-numeric(1000000);ix <- x < y; r[ix]<-x[ix];
r[!ix]<-y[!ix]; r})
>    user  system elapsed
>   0.082   0.053   0.135
>
> -pd
>
>
> > On 12 Jul 2019, at 15:02 , Richard O'Keefe <raoknz using gmail.com> wrote:
> >
> > "ifelse is very slow"?  Benchmark time.
> >> x <- runif(1000000)
> >> y <- runif(1000000)
> >> system.time(ifelse(x < y, x, y))
> >   user  system elapsed
> >  0.403   0.044   0.448
> >> system.time(y + (x < y)*(x - y))
> >   user  system elapsed
> >  0.026   0.012   0.038
> >
> > This appears to be a quality-of-implementation bug.
> >
> >
> > On Thu, 11 Jul 2019 at 04:14, Dénes Tóth <toth.denes using kogentum.hu> wrote:
> >
> >>
> >>
> >> On 7/10/19 5:54 PM, Richard O'Keefe wrote:
> >>> Expectation: ifelse will use the same "repeat vectors to match the
> >> longest"
> >>> rule that other vectorised functions do.  So
> >>> a <- 1:5
> >>> b <- c(2,3)
> >>> ifelse(a < 3, 1, b)
> >>> => ifelse(T T F F F <<5>>, 1 <<1>>, 2 3 <<2>>)
> >>> => ifelse(T T F F F <<5>>, 1 1 1 1 1 <<5>>, 2 3 2 3 2 <<5>>)
> >>> => 1 1 2 3 2
> >>> and that is indeed the answer you get.  Entirely predictable and
> >> consistent
> >>> with
> >>> other basic operations in R.
> >>>
> >>> The only tricky thing I see is that R has
> >>> a strict vectorised  ifelse(logical.vector, some.vector,
another.vector)
> >>> AND
> >>> a non-strict non-vectorised if (logical.scalar) some.value else
> >>> another.value
> >>> AND
> >>> a statement form if (logical.scalar) stmt.1; else stmt.2;
> >>
> >> Just for the records, there is a further form:
> >> `if`(logical.scalar, stmt.1, stmt.2)
> >>
> >> The main problem with ifelse is that 1) it is very slow, and 2) the
mode
> >> of its return value can be unintuitive or not too predictable (see also
> >> the Value and Warning sections of ?ifelse). One has to be very careful
> >> and ensure that 'yes' and 'no' vectors have the same class, because
> >> ifelse will not warn you at all:
> >>> ifelse(c(TRUE, TRUE), 1:2, LETTERS[1:2])
> >> [1] 1 2
> >>> ifelse(c(TRUE, FALSE), 1:2, LETTERS[1:2])
> >> [1] "1" "B"
> >>
> >> For options instead of base::ifelse, you might find this discussion
> >> helpful:
> >> https://github.com/Rdatatable/data.table/issues/3657
> >>
> >>
> >> Cheers,
> >> Denes
> >>
> >>
> >>>
> >>>
> >>> On Thu, 11 Jul 2019 at 01:47, Eric Berger <ericjberger using gmail.com>
wrote:
> >>>
> >>>> For example, can you predict what the following code will do?
> >>>>> a <- 1:5
> >>>>> b <- c(2,3)
> >>>>> ifelse( a < 3, 1, b)
> >>>>
> >>>>
> >>>> On Wed, Jul 10, 2019 at 4:34 PM José María Mateos <
chema using rinzewind.org>
> >>>> wrote:
> >>>>
> >>>>> On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
> >>>>>> 1. The ifelse() command is a bit tricky in R. Avoiding it is often
a
> >>>> good
> >>>>>> policy.
> >>>>>
> >>>>> You piqued my curiosity, can you elaborate a bit more on this?
> >>>>>
> >>>>> --
> >>>>> José María (Chema) Mateos || https://rinzewind.org
> >>>>>
> >>>>> ______________________________________________
> >>>>> R-help using r-project.org 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]]
> >>>>
> >>>> ______________________________________________
> >>>> R-help using r-project.org 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]]
> >>>
> >>> ______________________________________________
> >>> R-help using r-project.org 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]]
> >
> > ______________________________________________
> > R-help using r-project.org 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.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com
>
>
>
>
>
>
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list