ifelse - test (see the examples), and the class attribute...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
ifelse {base} R Documentation Conditional Element Selection Description ifelse returns a value with the same shape as test which is filled with elements selected from either yes or no depending on whether the element of test is TRUE or FALSE . Usage ifelse(test, yes, no) Arguments test an object which can be coerced to logical mode. yes return values for true elements of test . no return values for false elements of test . Details If yes or no are too short, their elements are recycled. yes will be evaluated if and only if any element of test is true, and analogously for no . Missing values in test give missing values in the result. Value A vector of the same length and attributes (including dimensions and "class" ) as test and data values from the values of yes or no . The mode of the answer will be coerced from logical to accommodate first any values taken from yes and then any values taken from no . Warning The mode of the result may depend on the value of
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: test (see the examples), and the class attribute (see oldClass ) of the result is taken from test and may be inappropriate for the values selected from yes and no . Sometimes it is better to use a construction such as (tmp <- yes; tmp[!test] <- no[!test]; tmp) , possibly extended to handle missing values in test . References Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language . Wadsworth & Brooks/Cole. See Also if . Examples x <- c(6:-4) sqrt(x) #- gives warning sqrt(ifelse(x >= 0, x, NA)) # no warning ## Note: the following also gives the warning ! ifelse(x >= 0, sqrt(x), NA) ## example of different return modes: yes <- 1:3 no <- pi^(0:3) typeof(ifelse(NA, yes, no)) # logical typeof(ifelse(TRUE, yes, no)) # integer typeof(ifelse(FALSE, yes, no)) # double [Package base version 2.11.1 Index ]...
View Full Document

This note was uploaded on 01/14/2011 for the course STATS 102A 102A taught by Professor Jandeleeuw during the Fall '10 term at UCLA.

Page1 / 2

ifelse - test (see the examples), and the class attribute...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online