[R] conditional assignment

Simon Cullen cullens at tcd.ie
Mon Jan 26 20:46:44 CET 2004


On Mon, 26 Jan 2004 20:15:51 +0100, <uaca at alumni.uv.es> wrote:

> I want to conditionally operate on certain elements of a matrix, let me
> explain it with a simple vector example
>
>
>> z<- c(1, 2, 3)
>> zz <- c(0,0,0)
>> null <- (z > 2) & ( zz <- z)
>> zz
> [1] 1 2 3
>
> why zz is not (0, 0, 3) ?????
<snip>
>
> in the other hand, it curious that null has reasonable values
>
>> null
> [1] FALSE FALSE  TRUE

What you have done there is create a boolean vector, null, of the same  
length as z (and zz).

For instance:
(z > 2) & (zz <- z)
=(F F T) & (T T T) (as assignment - presumably - returns T)
=(F F T).

What will work is:
z <- c(1, 2, 3)
index <- z>2
zz <- z * index


-- 
SC

Simon Cullen
Room 3030
Dept. Of Economics
Trinity College Dublin

Ph. (608)3477
Email cullens at tcd.ie




More information about the R-help mailing list