Hello everyone, I am experiencing a unfathomable benaviour of "subset" on a data.frame. This is a minimal reproducable example. The data.frame cosists only of one column, which contains 10 ascending values (from 0.1 to 1). Subsetting for 0.1 is working (gives me one row), subsetting for 0.3 gives me zero rows? Doing the same with values from 1 to 10 is working out well (as expected). Beimischung=seq(0.1,1,0.1) man2=data.frame(Beimischung) subset(man2, Beimischung==0.3) #---> gives 0 rows subset(man2, Beimischung==0.1) ---> gives one row (as expected)# #also not working: man2$Beimischung3=man2$Beimischung*10 subset(man2, Beimischung3==3) --> gives 0 rows Does anybody have a clue for me? Thanks in advance Regards Elisabeth _______________ Dipl.-Ing. Elisabeth Eckst?dt Member of Academic Staff & PhD Student Technische Universit?t Dresden Faculty of Mechanical Engineering Institue of Power Engineering Chair of Building Energy Systems and Heat Supply Phone +49 351 463 34709 Fax +49 351 463 37076 Web http://tu-dresden.de/mw/iet/ew
strange behaviour when subsetting a data.frame
5 messages · Eckstädt, Elisabeth, Sarah Goslee, Simon Hayward +1 more
R FAQ 7.31. http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f On Thu, Nov 7, 2013 at 8:36 AM, Eckst?dt, Elisabeth
<elisabeth.eckstaedt at tu-dresden.de> wrote:
Hello everyone, I am experiencing a unfathomable benaviour of "subset" on a data.frame. This is a minimal reproducable example. The data.frame cosists only of one column, which contains 10 ascending values (from 0.1 to 1). Subsetting for 0.1 is working (gives me one row), subsetting for 0.3 gives me zero rows? Doing the same with values from 1 to 10 is working out well (as expected). Beimischung=seq(0.1,1,0.1) man2=data.frame(Beimischung) subset(man2, Beimischung==0.3) #---> gives 0 rows subset(man2, Beimischung==0.1) ---> gives one row (as expected)# #also not working: man2$Beimischung3=man2$Beimischung*10 subset(man2, Beimischung3==3) --> gives 0 rows Does anybody have a clue for me? Thanks in advance Regards Elisabeth
Yes, but notice that man2[3,] == .3 [1] FALSE This is because of issues of machine precision when dealing with floating points, not a problem in R. Comparisons for nearly equivalent numbers are done using all.equal() as shown below.
all.equal(man2[3,], .3)
[1] TRUE -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Eckst?dt, Elisabeth Sent: Thursday, November 07, 2013 8:37 AM To: r-help at R-project.org Subject: [R] strange behaviour when subsetting a data.frame Hello everyone, I am experiencing a unfathomable benaviour of "subset" on a data.frame. This is a minimal reproducable example. The data.frame cosists only of one column, which contains 10 ascending values (from 0.1 to 1). Subsetting for 0.1 is working (gives me one row), subsetting for 0.3 gives me zero rows? Doing the same with values from 1 to 10 is working out well (as expected). Beimischung=seq(0.1,1,0.1) man2=data.frame(Beimischung) subset(man2, Beimischung==0.3) #---> gives 0 rows subset(man2, Beimischung==0.1) ---> gives one row (as expected)# #also not working: man2$Beimischung3=man2$Beimischung*10 subset(man2, Beimischung3==3) --> gives 0 rows Does anybody have a clue for me? Thanks in advance Regards Elisabeth _______________ Dipl.-Ing. Elisabeth Eckst?dt Member of Academic Staff & PhD Student Technische Universit?t Dresden Faculty of Mechanical Engineering Institue of Power Engineering Chair of Building Energy Systems and Heat Supply Phone +49 351 463 34709 Fax +49 351 463 37076 Web http://tu-dresden.de/mw/iet/ew ______________________________________________ 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.
So is there a way to make the subsetting behave as expected? -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Doran, Harold Sent: 07 November 2013 16:52 To: 'Eckst?dt, Elisabeth'; r-help at R-project.org Subject: Re: [R] strange behaviour when subsetting a data.frame Yes, but notice that man2[3,] == .3 [1] FALSE This is because of issues of machine precision when dealing with floating points, not a problem in R. Comparisons for nearly equivalent numbers are done using all.equal() as shown below.
all.equal(man2[3,], .3)
[1] TRUE -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Eckst?dt, Elisabeth Sent: Thursday, November 07, 2013 8:37 AM To: r-help at R-project.org Subject: [R] strange behaviour when subsetting a data.frame Hello everyone, I am experiencing a unfathomable benaviour of "subset" on a data.frame. This is a minimal reproducable example. The data.frame cosists only of one column, which contains 10 ascending values (from 0.1 to 1). Subsetting for 0.1 is working (gives me one row), subsetting for 0.3 gives me zero rows? Doing the same with values from 1 to 10 is working out well (as expected). Beimischung=seq(0.1,1,0.1) man2=data.frame(Beimischung) subset(man2, Beimischung==0.3) #---> gives 0 rows subset(man2, Beimischung==0.1) ---> gives one row (as expected)# #also not working: man2$Beimischung3=man2$Beimischung*10 subset(man2, Beimischung3==3) --> gives 0 rows Does anybody have a clue for me? Thanks in advance Regards Elisabeth _______________ Dipl.-Ing. Elisabeth Eckst?dt Member of Academic Staff & PhD Student Technische Universit?t Dresden Faculty of Mechanical Engineering Institue of Power Engineering Chair of Building Energy Systems and Heat Supply Phone +49 351 463 34709 Fax +49 351 463 37076 Web http://tu-dresden.de/mw/iet/ew ______________________________________________ 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. ______________________________________________ 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.
Yes, what I would do is write a function around the all.equal() comparisons on the data to see which values are "nearly" identical to the ones desired I the subset. If TRUE, I would retain those values and discard the others. -----Original Message----- From: Simon Hayward [mailto:simon.hayward at infinitycloud.com] Sent: Thursday, November 07, 2013 11:59 AM To: Doran, Harold; 'Eckst?dt, Elisabeth'; r-help at R-project.org Subject: RE: strange behaviour when subsetting a data.frame So is there a way to make the subsetting behave as expected? -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Doran, Harold Sent: 07 November 2013 16:52 To: 'Eckst?dt, Elisabeth'; r-help at R-project.org Subject: Re: [R] strange behaviour when subsetting a data.frame Yes, but notice that man2[3,] == .3 [1] FALSE This is because of issues of machine precision when dealing with floating points, not a problem in R. Comparisons for nearly equivalent numbers are done using all.equal() as shown below.
all.equal(man2[3,], .3)
[1] TRUE -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Eckst?dt, Elisabeth Sent: Thursday, November 07, 2013 8:37 AM To: r-help at R-project.org Subject: [R] strange behaviour when subsetting a data.frame Hello everyone, I am experiencing a unfathomable benaviour of "subset" on a data.frame. This is a minimal reproducable example. The data.frame cosists only of one column, which contains 10 ascending values (from 0.1 to 1). Subsetting for 0.1 is working (gives me one row), subsetting for 0.3 gives me zero rows? Doing the same with values from 1 to 10 is working out well (as expected). Beimischung=seq(0.1,1,0.1) man2=data.frame(Beimischung) subset(man2, Beimischung==0.3) #---> gives 0 rows subset(man2, Beimischung==0.1) ---> gives one row (as expected)# #also not working: man2$Beimischung3=man2$Beimischung*10 subset(man2, Beimischung3==3) --> gives 0 rows Does anybody have a clue for me? Thanks in advance Regards Elisabeth _______________ Dipl.-Ing. Elisabeth Eckst?dt Member of Academic Staff & PhD Student Technische Universit?t Dresden Faculty of Mechanical Engineering Institue of Power Engineering Chair of Building Energy Systems and Heat Supply Phone +49 351 463 34709 Fax +49 351 463 37076 Web http://tu-dresden.de/mw/iet/ew ______________________________________________ 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. ______________________________________________ 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.