White.Denis@epamail.epa.gov
2005-Jan-13 20:42 UTC
[R] zero index and lazy evaluation in ifelse()
I don't understand this behavior:> a <- c(0, 1, 2, 3) > b <- c(1, 2, 3, 4) > ifelse (a == 0, 0, b[a])[1] 0 2 3 1 rather than the desired 0 1 2 3. Thanks for any explanation.
> Date: Thu, 13 Jan 2005 12:42:59 -0800 > From: White.Denis at epamail.epa.gov > > I don't understand this behavior: > > > a <- c(0, 1, 2, 3) > > b <- c(1, 2, 3, 4) > > ifelse (a == 0, 0, b[a]) > [1] 0 2 3 1 > > rather than the desired 0 1 2 3. Thanks for any explanation. >Look at b[a]. Ray
Dear Denis, b[a] gives you c(1, 2, 3) (try it), which is recycled as c(1, 2, 3, 1); the elements in positions 2, 3, and 4 of this vector (i.e., where a != 0) are 2, 3, 1. Regards, John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox --------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > White.Denis at epamail.epa.gov > Sent: Thursday, January 13, 2005 3:43 PM > To: r-help at stat.math.ethz.ch > Subject: [R] zero index and lazy evaluation in ifelse() > > I don't understand this behavior: > > > a <- c(0, 1, 2, 3) > > b <- c(1, 2, 3, 4) > > ifelse (a == 0, 0, b[a]) > [1] 0 2 3 1 > > rather than the desired 0 1 2 3. Thanks for any explanation. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
White.Denis at epamail.epa.gov writes:> I don't understand this behavior: > > > a <- c(0, 1, 2, 3) > > b <- c(1, 2, 3, 4) > > ifelse (a == 0, 0, b[a]) > [1] 0 2 3 1 > > rather than the desired 0 1 2 3. Thanks for any explanation.b[a] is c(1,2,3), recycling to length 4 gives c(1,2,3,1), get it? -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
White.Denis@epamail.epa.gov
2005-Jan-13 22:44 UTC
[R] zero index and lazy evaluation in ifelse()
Thanks to all for clarifying that it isn't an element by element test and evaluation, rather an "any" test as the code says. Peter Dalgaard <p.dalgaard at biostat.ku.dk> wrote on 2005-01-13 14:22:50:> White.Denis at epamail.epa.gov writes: > > > This seems to contradict the help file. > > > > "... 'yes' will be evaluated if and only if any element of 'test' is > > true, > > and analogously for 'no'..." > > > > It doesn't. Read closer. Both 'yes' and 'no' is evaluated. The > former is c(0, 0, 0, 0) the latter is c(1, 2, 3, 1). > > > > Peter Dalgaard <p.dalgaard at biostat.ku.dk> wrote on 2005-01-1313:24:31:> > > > > White.Denis at epamail.epa.gov writes: > > > > > > > I don't understand this behavior: > > > > > > > > > a <- c(0, 1, 2, 3) > > > > > b <- c(1, 2, 3, 4) > > > > > ifelse (a == 0, 0, b[a]) > > > > [1] 0 2 3 1 > > > > > > > > rather than the desired 0 1 2 3. Thanks for any explanation. > > > > > > b[a] is c(1,2,3), recycling to length 4 gives c(1,2,3,1), get it? > > >