Skip to content
Back to formatted view

Raw Message

Message-ID: <1384916179.52321.YahooMailNeo@web142601.mail.bf1.yahoo.com>
Date: 2013-11-20T02:56:19Z
From: arun
Subject: if else in R
In-Reply-To: <CAEVDvzV76YjfnuOi-_xkp9h=6sioAQy+oa3UeYa=6KBV6XibTQ@mail.gmail.com>

Hi,
Try:
within(ab, {c <- ifelse(b>0, a/b,0)})
A.K.




On Tuesday, November 19, 2013 8:51 PM, Gary Dong <pdxgary163 at gmail.com> wrote:
Dear R users,

I am a R beginner and I am having trouble in using "If Else" in R. Here is
an example:

## create a data frame

a<-c(1,2,3,4)
b<-c(2,0,5,0)
ab<-data.frame(cbind(a,b))

##calculate c, which is the ratio between a and b

if(ab$b>0) {
ab$c<-ab$a/ab$b
} else {
ab$c<-0
}

here is the error I got:

Warning message:
In if (ab$b > 0) { :
? the condition has length > 1 and only the first element will be used.

Any help is appreciated!

Gary

??? [[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.