Hi all 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) ????? the null <- assignment is to keep the console silent in the other hand, it curious that null has reasonable values> null[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:> > Hi all > > 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) ????? >Break it down into bits:> z<- c(1, 2, 3) > (z > 2)[1] FALSE FALSE TRUE> ( zz <- z)[1] 1 2 3> (z > 2) & ( zz <- z)[1] FALSE FALSE TRUE> TRUE & ( zz <- z)[1] TRUE TRUE TRUE> rep(TRUE,4) & ( zz <- z)[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:> zz <- ifelse(z > 2, z, 0) > zz[1] 0 0 3 if that's what you want.> > the null <- assignment is to keep the console silent > > in the other hand, it curious that null has reasonable values > > > null > [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 <--- > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Roger Bivand Econonic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway, voice: +47-55959355, fax: +47-55959393; Roger.Bivand at nhh.no
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 TRUEWhat 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
> From: Simon Cullen > 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)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't think so. zz <- z has the value z; i.e., c(1, 2, 3). When evaluated as logicals, non-zero values are treated as true (as in C), I believe. For example:> z <- rep(0, 3) > ifelse(zz <- z, 1, 0)[1] 0 0 0> (zz <- z) == TRUE[1] FALSE FALSE FALSE However, what tripped me is the fact that even though non-zero is logically `true', it's not necessarily equal to TRUE (which is numerically equal to 1):> z <- 0:2 > (zz <- z) == TRUE[1] FALSE TRUE FALSE> ifelse(zz <- z, 1, 0)[1] 0 1 1> if(3) TRUE else FALSE[1] TRUE Andy> =(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------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}
On Mon, 26 Jan 2004, Simon Cullen wrote:> 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)assignment returns the new value of zz, which as it is all non-zero coerces to the logical vector T T T> =(F F T). > > What will work is: > z <- c(1, 2, 3) > index <- z>2 > zz <- z * indexRather better I think is zz <- ifelse(z > 2, z, zz) or even ind <- z > 2 zz[ind] <- z[ind] -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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:> > Hi all > > 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) ????? > > > the null <- assignment is to keep the console silent > > in the other hand, it curious that null has reasonable values > > > null > [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 <--- >-- 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 <---