Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

ifelse1

Conditional Data Selection


Description

This is equivalent to {if(test) x else y} . The main advantage of using this function is better formatting, and a more natural syntax when the result is being assigned; see examples below.

With 5 arguments, this is equivalent to {if(test1) x else if(test2) u else v} (where arguments are given by name, not position).

Usage

ifelse1(test, x, y, ...)

Arguments

test

logical value; if TRUE return x.

x

any object; this is returned if test is TRUE.

y

any object; this is returned if test is FALSE.

...

there should be 3, 5, 7, etc. arguments to this function; arguments 1, 3, 5, etc. should be logical values; the other arguments (even numbered, and last) are objects that may be returned.

Details

test should be a scalar logical, and only one of x or y is evaluated, depending on whether test = TRUE or test = FALSE, and x and y may be any objects. In contrast, for ifelse, test is normally a vector, both x and y are evaluated, even if not used, and x and y are vectors the same length as test.

Value

with three arguments, one of x or y. With k arguments, one of arguments 2, 4, ..., k-1, k.

See Also

Examples

ifelse1(TRUE, "cat", "dog")
ifelse1(FALSE, "one", FALSE, "two", "three")

splus2R

Supplemental S-PLUS Functionality in R

v1.3-3
GPL-2
Authors
William Constantine [aut], Tim Hesterberg [aut], Knut Wittkowski [ctb], Tingting Song [ctb], Bill Dunlap [ctb], Stephen Kaluzny [ctb, cre]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.