Skip to content
Prev 311405 / 398513 Next

Using if

Tena koe

If the vectors are long and time is an issue, it might be better to avoid the use of ifelse().

Try:
set.seed(0)
EvHint <- rbinom(10^7, 1, 0.5)
MinTex <- rbinom(10^7, 1, 0.5)
system.time(RiskTest0 <- ifelse(EvHint==1 & MinTex==1, 1, 0))
system.time({
RiskTest1 <- rep(0, length(EvHint))
RiskTest1[EvHint==1 & MinTex==1] <- 1})
all.equal(RiskTest0, RiskTest1)

Hei kona ra ....

Peter Alspach

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jeff Newmiller
Sent: Wednesday, 21 November 2012 8:18 a.m.
To: Virgile Capo-Chichi; r-help at r-project.org
Subject: Re: [R] Using if

?ifelse

Quite distinct from if () {} else {}.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Virgile Capo-Chichi <vcapochichi at gmail.com> wrote:

            
______________________________________________
R-help at r-project.org mailing list
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.

The contents of this e-mail are confidential and may be ...{{dropped:14}}