[R] Example Syntax for if - then- and - else Statements

Nordlund, Dan (DSHS) NordlDJ at dshs.wa.gov
Wed May 17 20:01:49 CEST 2006


> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-
> bounces at stat.math.ethz.ch] On Behalf Of Lanre Okusanya
> Sent: Wednesday, May 17, 2006 10:17 AM
> To: 'r-help at stat.math.ethz.ch'
> Subject: [R] Example Syntax for if - then- and - else Statements
> 
> Hello R users,
> 
> I am a fairly new R user, however, one of the problems I am having is
> the use of applying if-, and-, then-, else- statements in R against
> datasets & dataframe. Rtips mentions it, but without simple examples I
> am not quite sure what my syntax should look like. I am particularly
> interested in syntax that can be used with the transform function. for
> example
> 
>     if foo[,1] == 'n', foo[,2] == 4, else foo[,2] == 6
> 
> Could someone please share simple example codes with different variants
> on the theme.
> thanks.
> 
> --
> Lanre Okusanya, Pharm.D.,BCPS
> UB/Pfizer Pharmacometrics Fellow
> University at Buffalo School of Pharmacy and Pharmaceutical Sciences
> 237 Cooke Hall
> Buffalo, NY 14260
> Email: ooo at buffalo.edu
> Tel: (716)645-2828 x 275
> Fax: (716)645-2886
> 

Look at the help files

?'if' and ?ifelse

For your particular example, try something like this:   

>foo[,2] <- ifelse(foo[,1] == 'n', 4, 6)



Hope this is helpful,

Dan

Daniel J. Nordlund
Research and Data Analysis
Washington State Department of Social and Health Services
Olympia, WA  98504-5204




More information about the R-help mailing list