[R] if/else construct

Charilaos Skiadas skiadas at hanover.edu
Fri Apr 13 15:19:07 CEST 2007


Corinna,
On Apr 13, 2007, at 8:19 AM, Schmitt, Corinna wrote:

> Dear R-Experts,
>
> Since Monday I try to write the right if/else construct for the  
> program.
> I was not successful yet. Important is the order of the if-cases! The
> code is running with the 2 if-cases. An if/else construction would  
> look
> better, so can any one help me with the right if/else construction?

There are three possible values for deletingDecision:

1) "y"
2) "n"
3) Something else

If you are going to use an if/else construct, you better make sure  
you know what you want to do with the third option. In my opinion,  
you would want it to be the same as 2, in which case you really want  
the "if decision==yes" option first, and the other option in the else  
clause. Looking at ?"if", it should look at something like this:

if (deletingDecision == yes) {
	print("Yes!")
} else {
	print("Not yes!")
}

Or if you really want a third option:

if (deletingDecision == yes) {
	print("Yes!")
} else if (deletingDecision == no) {
	print("No!")
} else {
	print("Other!")
}

> Thanks, Corinna
> Program:
> --------
>
> deletingDecision = userInput()
> yes <- c("y")
> no <- c("n")
> noAnswer <- c("Current R workspace was not deleted!")
>
> # first if
> if (deletingDecision == no) {
>     print("Current R workspace was not deleted!")
>     }
>
> # second if
> if (deletingDecision == yes) {
>     rm(list=ls(all=TRUE))
>     print("Current R workspace was deleted!")
>     }
>

Hope this helps.

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College



More information about the R-help mailing list