[R] Problem getting an ifelse statment to work

Richard A. O'Keefe ok at cs.otago.ac.nz
Tue Oct 7 06:18:47 CEST 2003


Something I see fairly often in R code is heavy use of 'return',
like this example today:

	> decision <- function(a, b) {
	+     if (a == 1 || b == 1) return(1)
	+     if (a == 2 || b == 2) return(2)
	+     if (a == 3 || b == 3) return(3)
	+     if (a == 4 || b == 4) return(4)
	+     NA
	+ }

Why do people write code like that instead of

    decision <- function (a,b) {
	if (a == 1 || b == 1) 1 else
	if (a == 2 || b == 2) 2 else
	if (a == 3 || b == 3) 3 else
	if (a == 4 || b == 4) 4 else
	NA
    }

	> mapply(decision, qs2, qs9)
	 [1] 2 1 1 3 4 3 1 1 1 4 1 2 3 1 4 3 1 2 2 3
	
In this case, what's wrong with pmin(qs2, qs9)?




More information about the R-help mailing list