I have come across some odd behavior (to me) using the abs() function that I wonder if anyone can explain. Using R version 3.2.0, I created a vector of absolute values using the following code:> tran<-c(7.2) > tgrid<-c(7.1,7.4,7.3,7.1,7.3) > dgrid<-abs(tgrid-tran) > dgrid[1] 0.1 0.2 0.1 0.1 0.1 When I tried to extract just the rows with values>0.1, I get> dgrid[dgrid>0.1][1] 0.1 0.2 0.1 There should be only 1 value extracted. However, if I enter the values by hand> bgrid<-c(0.1,0.2,0.1,0.1,0.1) > bgrid[1] 0.1 0.2 0.1 0.1 0.1> bgrid[bgrid>0.1][1] 0.2 The result is correct. So why is this happening? I did explore a little bit and found> as.character(dgrid)[1] "0.100000000000001" "0.2" [3] "0.0999999999999996" "0.100000000000001" [5] "0.0999999999999996" which shows the absolute values of the negative differences are slightly greater than the difference of 0.1 Is this normal behavior for the abs() function? Thanks, Gary Nelson [[alternative HTML version deleted]]
Please read R FAQ 7.31. On Fri, Jun 12, 2015 at 11:39 AM, Nelson, Gary (MISC) <gary.nelson at state.ma.us> wrote:> I have come across some odd behavior (to me) using the abs() function that I wonder if anyone can explain. > > Using R version 3.2.0, I created a vector of absolute values using the following code: > >> tran<-c(7.2) >> tgrid<-c(7.1,7.4,7.3,7.1,7.3) >> dgrid<-abs(tgrid-tran) >> dgrid > [1] 0.1 0.2 0.1 0.1 0.1 > > When I tried to extract just the rows with values>0.1, I get > >> dgrid[dgrid>0.1] > [1] 0.1 0.2 0.1 > > There should be only 1 value extracted. > > However, if I enter the values by hand > >> bgrid<-c(0.1,0.2,0.1,0.1,0.1) >> bgrid > [1] 0.1 0.2 0.1 0.1 0.1 >> bgrid[bgrid>0.1] > [1] 0.2 > > The result is correct. So why is this happening? > > I did explore a little bit and found > >> as.character(dgrid) > [1] "0.100000000000001" "0.2" > [3] "0.0999999999999996" "0.100000000000001" > [5] "0.0999999999999996" > > which shows the absolute values of the negative differences are slightly greater than the difference of 0.1 > > Is this normal behavior for the abs() function? > > Thanks, > > Gary Nelson-- Sarah Goslee http://www.functionaldiversity.org
FAQ 7.31 --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On June 12, 2015 8:39:48 AM PDT, "Nelson, Gary (MISC)" <gary.nelson at state.ma.us> wrote:>I have come across some odd behavior (to me) using the abs() function >that I wonder if anyone can explain. > >Using R version 3.2.0, I created a vector of absolute values using >the following code: > >> tran<-c(7.2) >> tgrid<-c(7.1,7.4,7.3,7.1,7.3) >> dgrid<-abs(tgrid-tran) >> dgrid >[1] 0.1 0.2 0.1 0.1 0.1 > >When I tried to extract just the rows with values>0.1, I get > >> dgrid[dgrid>0.1] >[1] 0.1 0.2 0.1 > >There should be only 1 value extracted. > >However, if I enter the values by hand > >> bgrid<-c(0.1,0.2,0.1,0.1,0.1) >> bgrid >[1] 0.1 0.2 0.1 0.1 0.1 >> bgrid[bgrid>0.1] >[1] 0.2 > >The result is correct. So why is this happening? > >I did explore a little bit and found > >> as.character(dgrid) >[1] "0.100000000000001" "0.2" >[3] "0.0999999999999996" "0.100000000000001" >[5] "0.0999999999999996" > >which shows the absolute values of the negative differences are >slightly greater than the difference of 0.1 > >Is this normal behavior for the abs() function? > >Thanks, > >Gary Nelson > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.
Hi, Just do the following:> tran<-c(7.2) > tgrid<-c(7.1,7.4,7.3,7.1,7.3) > tgrid<-tgrid-tran > tgrid[1] -0.1 0.2 0.1 -0.1 0.1> abs(tgrid[tgrid>0.1])[1] 0.2 Andr?s> El 12/06/2015, a las 11:01, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> escribi?: > > FAQ 7.31 > --------------------------------------------------------------------------- > Jeff Newmiller The ..... ..... Go Live... > DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... > Live: OO#.. Dead: OO#.. Playing > Research Engineer (Solar/Batteries O.O#. #.O#. with > /Software/Embedded Controllers) .OO#. .OO#. rocks...1k > --------------------------------------------------------------------------- > Sent from my phone. Please excuse my brevity. > > On June 12, 2015 8:39:48 AM PDT, "Nelson, Gary (MISC)" <gary.nelson at state.ma.us> wrote: >> I have come across some odd behavior (to me) using the abs() function >> that I wonder if anyone can explain. >> >> Using R version 3.2.0, I created a vector of absolute values using >> the following code: >> >>> tran<-c(7.2) >>> tgrid<-c(7.1,7.4,7.3,7.1,7.3) >>> dgrid<-abs(tgrid-tran) >>> dgrid >> [1] 0.1 0.2 0.1 0.1 0.1 >> >> When I tried to extract just the rows with values>0.1, I get >> >>> dgrid[dgrid>0.1] >> [1] 0.1 0.2 0.1 >> >> There should be only 1 value extracted. >> >> However, if I enter the values by hand >> >>> bgrid<-c(0.1,0.2,0.1,0.1,0.1) >>> bgrid >> [1] 0.1 0.2 0.1 0.1 0.1 >>> bgrid[bgrid>0.1] >> [1] 0.2 >> >> The result is correct. So why is this happening? >> >> I did explore a little bit and found >> >>> as.character(dgrid) >> [1] "0.100000000000001" "0.2" >> [3] "0.0999999999999996" "0.100000000000001" >> [5] "0.0999999999999996" >> >> which shows the absolute values of the negative differences are >> slightly greater than the difference of 0.1 >> >> Is this normal behavior for the abs() function? >> >> Thanks, >> >> Gary Nelson >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.