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:
Dear R users, As a new comer to R, I would like to create a new variable using if statements but don't know how to do it. Basically, I have two variables (EvHint and MinTex). I want to create a third variable called RiskTest. In SPSS, my syntax would look like Compute RiskTest=0. if (EvHint=1 & MinTex=1) RiskTest=1. Question: How do I do this with R? My Data EvHint<-c(0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0) MinTex<-c(0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0) Thanks, V [[alternative HTML version deleted]]
______________________________________________ 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.
______________________________________________ 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}}