Hi all, I am quite sure it's not a bug, but I am going nuts about this. I do not possibly understand why I get different results for b1 and b2 as shown below. x=c(183,191,192,193,195,206,207,209,210,211,212,213,214,217,218,221,222,223, 224,225,227,228,229,230) y=c(221,225,228,241,242) z=y[y<max(x)] ##all Ys smaller than the maximum of X a=ifelse(length(z)==0,NA,max(z)) ##assign NA if length of z is zero, take the max of z otherwise b1=ifelse(is.numeric(a)==T,x[x>a],x) ##if a is numeric, assign all x that are greater than a; otherwise assign the entire x b2=x[x>a] ## assign all x greater than a b1 and b2 should be equal as I see it. Anybody who can explain why they are not makes my day, especially the one who can tell me how I can get b1 equal to b2 (without sacrificing the ifelse condition). Thanks much, Daniel ------------------------- cuncta stricte discussurus
On 10/10/2008, at 10:33 AM, Daniel Malter wrote:> Hi all, I am quite sure it's not a bug, but I am going nuts about > this. I do > not possibly understand why I get different results for b1 and b2 > as shown > below. > > x=c > (183,191,192,193,195,206,207,209,210,211,212,213,214,217,218,221,222,2 > 23, > 224,225,227,228,229,230) > > y=c(221,225,228,241,242) > > z=y[y<max(x)] ##all Ys smaller than the maximum of X > > a=ifelse(length(z)==0,NA,max(z)) ##assign NA if length of z is > zero, take > the max of z otherwise > > b1=ifelse(is.numeric(a)==T,x[x>a],x) ##if a is numeric, assign all > x that > are greater than a; otherwise assign the entire x > > b2=x[x>a] ## assign all x greater than a > > > b1 and b2 should be equal as I see it. Anybody who can explain why > they are > not makes my day, especially the one who can tell me how I can get > b1 equal > to b2 (without sacrificing the ifelse condition).Shouldn't there be an FAQ discussing the difference between ifelse() and if{ } else { }? I couldn't find one, and this issue seems to arise over and OVER again on this list. Unless their noses are rubbed in it, many people seem not to grok the distinction. cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
I just gave up a second too early: It does not work with ifelse(...) as ifelse(...) assigns only a singley value (the first in the vector). A regular if(...) else ... condition works, however. Daniel Malter wrote:> > Hi all, I am quite sure it's not a bug, but I am going nuts about this. I > do > not possibly understand why I get different results for b1 and b2 as shown > below. > > x=c(183,191,192,193,195,206,207,209,210,211,212,213,214,217,218,221,222,223, > 224,225,227,228,229,230) > > y=c(221,225,228,241,242) > > z=y[y<max(x)] ##all Ys smaller than the maximum of X > > a=ifelse(length(z)==0,NA,max(z)) ##assign NA if length of z is zero, take > the max of z otherwise > > b1=ifelse(is.numeric(a)==T,x[x>a],x) ##if a is numeric, assign all x that > are greater than a; otherwise assign the entire x > > b2=x[x>a] ## assign all x greater than a > > > b1 and b2 should be equal as I see it. Anybody who can explain why they > are > not makes my day, especially the one who can tell me how I can get b1 > equal > to b2 (without sacrificing the ifelse condition). > > Thanks much, > Daniel > > ------------------------- > cuncta stricte discussurus > > ______________________________________________ > 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://www.nabble.com/Bug-in-ifelse-tp19907970p19908084.html Sent from the R help mailing list archive at Nabble.com.