Hi, Is there any equivalent for ifelse (except if (cond) expr1 else expr2) which takes an atomic element as argument but returns vector since ifelse returns an object of the same length as its argument? x = c(1,2,3) y = c(4,5,6,7) z = 3 ifelse(z <= 3,x,y) would return x and not 1 thanks
Try this: list('TRUE' = x, 'FALSE' = y)[[as.character(as.name(z <= 1))]] On Wed, Dec 9, 2009 at 3:40 PM, carol white <wht_crl at yahoo.com> wrote:> Hi, > Is there any equivalent for ifelse (except if (cond) expr1 else expr2) which takes an atomic element as argument but returns vector since ifelse returns an object of the same length as its argument? > > x = c(1,2,3) > y = c(4,5,6,7) > z = 3 > > ifelse(z <= 3,x,y) > > would return x and not 1 > > thanks > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
On Dec 9, 2009, at 12:40 PM, carol white wrote:> Hi, > Is there any equivalent for ifelse (except if (cond) expr1 else > expr2) which takes an atomic element as argument but returns vector > since ifelse returns an object of the same length as its argument? > > x = c(1,2,3) > y = c(4,5,6,7) > z = 3 > > ifelse(z <= 3,x,y) > > would return x and not 1I worry that this is too simple, so wonder if you have expressed your intent clearly. > if(z <= 3) {x} else {y} [1] 1 2 3>David Winsemius, MD Heritage Laboratories West Hartford, CT
On 09/12/2009 12:40 PM, carol white wrote:> Hi, > Is there any equivalent for ifelse (except if (cond) expr1 else expr2) which takes an atomic element as argument but returns vector since ifelse returns an object of the same length as its argument? >I don't understand what's wrong with "if (cond) expr1 else expr2". It can be used in an expression, e.g. w <- if (z <= 3) x else y which is I think exactly what you are asking for. Duncan Murdoch> x = c(1,2,3) > y = c(4,5,6,7) > z = 3 > > ifelse(z <= 3,x,y) > > would return x and not 1 > > thanks > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
David Winsemius wrote:> > > On Dec 9, 2009, at 12:40 PM, carol white wrote: > >> Hi, >> Is there any equivalent for ifelse (except if (cond) expr1 else >> expr2) which takes an atomic element as argument but returns vector >> since ifelse returns an object of the same length as its argument? >> >> x = c(1,2,3) >> y = c(4,5,6,7) >> z = 3 >> >> ifelse(z <= 3,x,y) >> >> would return x and not 1 > > I worry that this is too simple, so wonder if you have expressed your > intent clearly. > > > if(z <= 3) {x} else {y} > [1] 1 2 3 >> > > I was wondering David, why is the {} necessary? > if(z <= 3) x else y > [1] 1 2 3 > > since without {} it cames with the same result? > > Thanks > MR. > > > > David Winsemius, MD > Heritage Laboratories > West Hartford, CT > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://n4.nabble.com/equivalent-of-ifelse-tp956232p956258.html Sent from the R help mailing list archive at Nabble.com.