Main /

Replacing Withifelse

edit SideBar

Replacing Withifelse

Use ifelse() to replace values in a matrix.

From R Help:

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.

Example:

> x=matrix(c(1,1,1,1,-1,-1,-1,-1),ncol=2)
> x
     [,1] [,2]
[1,]    1   -1
[2,]    1   -1
[3,]    1   -1
[4,]    1   -1
> x=ifelse(x==-1,0,x)
> x
     [,1] [,2]
[1,]    1    0
[2,]    1    0
[3,]    1    0
[4,]    1    0
Recent Changes (All) | Edit SideBar Page last modified on December 01, 2006, at 10:27 PM Edit Page | Page History
Powered by PmWiki