Hi R-Helpers, I don't think I need to post a dataset for this question but if I do, I can. Anyway, I am having a lot of trouble with the ifelse command. Here is my code: vn$PM.DIST_flag <- ifelse( (vn$PM.EXP > 0.0) & (vn$PM.DIST.TOT != 1.0), 1, 0 ) And here is my output that doesn't make ANY sense: PM.EXP PM.DIST.TOT PM.DIST_flag 0 0 0 0 0 0 0 0 0 177502 1 0 31403 1 0 0 0 0 1100549 1 0 38762 1 0 0 0 0 20025 1 0 0 0 0 13742 1 0 0 0 0 83078 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 165114 1 0 0 0 0 417313 1 0 3546 1 0 4613 1 0 225460 1 0 6417 1 1 23 1 0 3402 1 0 8504 1 1 8552 1 0 9723 1 0 37273 1 1 396 1 0 1478 1 0 2074 1 0 12220 1 1 97691 2 1 0 0 0 33993 2 1 As you can see, there are many instances where PM.EXP > 0 and PM.DIST.TOT 1 yet PM.DIST_flag = 1 and it should be 0. It should only flag in cases such as the last line of data. WWHHHYYYYYYYY???? Why why why why why why why? Why? (Sorry, I've been trying to figure this out for hours and I've devolved to mumbling in corners and banging my head against the table) What in the world am I doing wrong? Or is ifelse not the right function? Best, Jen [[alternative HTML version deleted]]
On Fri, Aug 24, 2012 at 3:22 PM, Jennifer Sabatier <plessthanpointohfive at gmail.com> wrote:> Hi R-Helpers, > > I don't think I need to post a dataset for this question but if I do, I > can. Anyway, I am having a lot of trouble with the ifelse command. >You probably should have: dput() makes it super easy as well.> Here is my code: > > > vn$PM.DIST_flag <- ifelse( (vn$PM.EXP > 0.0) & (vn$PM.DIST.TOT != 1.0), 1, > 0 ) > > > And here is my output that doesn't make ANY sense: > > PM.EXP PM.DIST.TOT PM.DIST_flag 0 0 0 0 0 0 0 0 0 177502 1 0 31403 1 > 0 0 0 0 1100549 1 0 38762 1 0 0 0 0 20025 1 0 0 0 0 13742 1 0 0 0 0 > 83078 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 > 165114 1 0 0 0 0 417313 1 0 3546 1 0 4613 1 0 225460 1 0 6417 1 1 23 > 1 0 3402 1 0 8504 1 1 8552 1 0 9723 1 0 37273 1 1 396 1 0 1478 1 0 > 2074 1 0 12220 1 1 97691 2 1 0 0 0 33993 2 1Indeed it makes no sense to me either because you sent HTML email which got mangled by the server.> > As you can see, there are many instances where PM.EXP > 0 and PM.DIST.TOT > 1 yet PM.DIST_flag = 1 and it should be 0. It should only flag in cases > such as the last line of data. > > WWHHHYYYYYYYY???? Why why why why why why why? Why? > > (Sorry, I've been trying to figure this out for hours and I've devolved to > mumbling in corners and banging my head against the table) > > What in the world am I doing wrong? Or is ifelse not the right function?First guess.... standard problems with equality of floating point numbers. (See R FAQ 7.31 for the details) You probably want to change x == 1 to abs(x - 1) < 1e-05 or something similar. Cheers, Michael> > Best, > > Jen > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
On 2012-08-24 13:22, Jennifer Sabatier wrote:> Hi R-Helpers, > > I don't think I need to post a dataset for this question but if I do, I > can. Anyway, I am having a lot of trouble with the ifelse command. > > Here is my code: > > > vn$PM.DIST_flag <- ifelse( (vn$PM.EXP > 0.0) & (vn$PM.DIST.TOT != 1.0), 1, > 0 ) > > > And here is my output that doesn't make ANY sense: > > PM.EXP PM.DIST.TOT PM.DIST_flag 0 0 0 0 0 0 0 0 0 177502 1 0 31403 1 > 0 0 0 0 1100549 1 0 38762 1 0 0 0 0 20025 1 0 0 0 0 13742 1 0 0 0 0 > 83078 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 > 165114 1 0 0 0 0 417313 1 0 3546 1 0 4613 1 0 225460 1 0 6417 1 1 23 > 1 0 3402 1 0 8504 1 1 8552 1 0 9723 1 0 37273 1 1 396 1 0 1478 1 0 > 2074 1 0 12220 1 1 97691 2 1 0 0 0 33993 2 1 > > As you can see, there are many instances where PM.EXP > 0 and PM.DIST.TOT > 1 yet PM.DIST_flag = 1 and it should be 0. It should only flag in cases > such as the last line of data._Many_ instances?? I see only 4 such cases. Still not good, though. Here's what you should do: 1. Don't send html mail. 2. use simple variable names (x,y,z would do fine here). 3. either provide the data or at least a part of it with dput() or at least provide str(vn). 4. when you're trying to decide between operator error and bug, go with the operator error theory. You'll be correct at least 99% of the time. I ran your command on the above (suitably deciphered) data and had no problem getting what I think you expect (i.e. the four suspect cases came out just as they should). But what my mailer provides as your data may not be what you really have. Oh, and get a bandage for that head bruise. Peter Ehlers> > WWHHHYYYYYYYY???? Why why why why why why why? Why? > > (Sorry, I've been trying to figure this out for hours and I've devolved to > mumbling in corners and banging my head against the table) > > What in the world am I doing wrong? Or is ifelse not the right function? > > Best, > > Jen > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Off the wall / wild guess, do you use attach() frequently? Not entirely sure how it would come up, but it tends to make weird errors like this occur. M On Fri, Aug 24, 2012 at 4:36 PM, Jennifer Sabatier <plessthanpointohfive at gmail.com> wrote:> Hi Rui, > > Thanks so much for responding but I think with my HTML problem the vn > data you made must not be the same. I tried running your code on the > data (I uploaded a copy) and I got the same thing I had before. > > Jen > > On Fri, Aug 24, 2012 at 5:28 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote: >> >> 165114 1 0 0 0 0 417313 1 0 3546 1 0 4613 1 0 225460 1 0 6417 1 1 23