Skip to content

conditional assignment

5 messages · uaca@alumni.uv.es, Roger Bivand, Simon Cullen +1 more

#
Hi all

I want to conditionally operate on certain elements of a matrix, let me
explain it with a simple vector example
[1] 1 2 3

why zz is not (0, 0, 3) ?????


the null <- assignment is to keep the console silent

in the other hand, it curious that null has reasonable values
[1] FALSE FALSE  TRUE

Thanks in advance

	Ulisses

                Debian GNU/Linux: a dream come true
-----------------------------------------------------------------------------
"Computers are useless. They can only give answers."            Pablo Picasso

Humans are slow, innaccurate, and brilliant.
Computers are fast, acurrate, and dumb. 
Together they are unbeatable

--->	Visita http://www.valux.org/ para saber acerca de la	<---
--->	Asociaci?n Valenciana de Usuarios de Linux		<---
#
On Mon, 26 Jan 2004 uaca at alumni.uv.es wrote:

            
Break it down into bits:
[1] FALSE FALSE  TRUE
[1] 1 2 3
[1] FALSE FALSE  TRUE
[1] TRUE TRUE TRUE
[1] TRUE TRUE TRUE TRUE
Warning message: 
longer object length
        is not a multiple of shorter object length in: rep(TRUE, 4) & (zz <- z) 

The first part is a logical vector, the second is the result of assigning 
z to zz, & of them isn't terribly meaningful?

Try:
[1] 0 0 3

if that's what you want.

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

            
<snip>
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
#
On Mon, 26 Jan 2004, Simon Cullen wrote:

            
assignment returns the new value of zz, which as it is all non-zero
coerces to the logical vector T T T
Rather better I think is

zz <- ifelse(z > 2, z, zz)

or even

ind <- z > 2
zz[ind] <- z[ind]
2 days later
#
Hi all again

I could not reply before because I was/am very busy

ifthenelse() is what I wanted, I have to read more carefully your
explanations to better understand it

Thanks everybody

	Ulisses
On Mon, Jan 26, 2004 at 08:15:51PM +0100, uaca at alumni.uv.es wrote: