Rounding behavior

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Rounding behavior

rlderi01
Hello,

Apologies if this is a simple misunderstanding.

round((.575*100),0) gives 57
round(57.5,0) gives 58

Why?

Ryan Derickson
University of Cincinnati

On Tue, Oct 9, 2018, 10:08 AM PIKAL Petr <[hidden email]> wrote:

> Hi
>
> You could use brute force approach. Just print out "file.names" and
> estimate ordering vector.
> In czech locale it is
>
> oo <- c(6, 11, 1, 4, 5, 2, 3, 10, 12, 9, 7, 8)
>
> In english locale it is different :-)
>
> After that
> file.names[oo]
>
> should give you correct order of file names
>
> Cheers
> Petr
>
> > -----Original Message-----
> > From: R-help <[hidden email]> On Behalf Of Ek Esawi
> > Sent: Tuesday, October 9, 2018 3:44 PM
> > To: [hidden email]
> > Subject: [R] Reorder file names read by list.files function
> >
> > Hi All--
> >
> > I used base R list.file function to read files from a directory. The
> file names are
> > months (April, August, etc). That's the system reads them in
> alphabetical order.,
> > but i want to reordered them in calendar order (January, February,
> > ...December).. I thought i might be able to do it via RegEx or possibly
> gtools
> > package, I am wondering if there is an easier way.
> >
> > Thanks--EK
> >
> > Example
> > path = "C:/Users/name/Downloads/MyFiles"
> > file.names <- dir(path, pattern =".PDF")
> >
> > Example output
> > Output:
> > "February.PDF"  "January.PDF" "March.PDF"
> > Desired output
> > "January.PDF"  "February.PDF" "March.PDF"
> >
> > ______________________________________________
> > [hidden email] 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.
> Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních
> partnerů PRECHEZA a.s. jsou zveřejněny na:
> https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information
> about processing and protection of business partner’s personal data are
> available on website:
> https://www.precheza.cz/en/personal-data-protection-principles/
> Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou
> důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení
> odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any
> documents attached to it may be confidential and are subject to the legally
> binding disclaimer: https://www.precheza.cz/en/01-disclaimer/
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Rounding behavior

Richard M. Heiberger
FAQ 7.31
Open this file in your favorite text editor on your computer
system.file("../../doc/FAQ")

57.5 comes out even in binary, and .575 does not.

> print(.575*100, digits=17)
[1] 57.499999999999993
## The fraction is less than .5, hence it gets rounded down to 57
> print(57.5, digits=17)
[1] 57.5
## the fraction is exactly .5, hence gets rounded to the nearest even number 58
l

On Tue, Oct 9, 2018 at 1:14 PM, Ryan Derickson <[hidden email]> wrote:

> Hello,
>
> Apologies if this is a simple misunderstanding.
>
> round((.575*100),0) gives 57
> round(57.5,0) gives 58
>
> Why?
>
> Ryan Derickson
> University of Cincinnati
>
> On Tue, Oct 9, 2018, 10:08 AM PIKAL Petr <[hidden email]> wrote:
>
>> Hi
>>
>> You could use brute force approach. Just print out "file.names" and
>> estimate ordering vector.
>> In czech locale it is
>>
>> oo <- c(6, 11, 1, 4, 5, 2, 3, 10, 12, 9, 7, 8)
>>
>> In english locale it is different :-)
>>
>> After that
>> file.names[oo]
>>
>> should give you correct order of file names
>>
>> Cheers
>> Petr
>>
>> > -----Original Message-----
>> > From: R-help <[hidden email]> On Behalf Of Ek Esawi
>> > Sent: Tuesday, October 9, 2018 3:44 PM
>> > To: [hidden email]
>> > Subject: [R] Reorder file names read by list.files function
>> >
>> > Hi All--
>> >
>> > I used base R list.file function to read files from a directory. The
>> file names are
>> > months (April, August, etc). That's the system reads them in
>> alphabetical order.,
>> > but i want to reordered them in calendar order (January, February,
>> > ...December).. I thought i might be able to do it via RegEx or possibly
>> gtools
>> > package, I am wondering if there is an easier way.
>> >
>> > Thanks--EK
>> >
>> > Example
>> > path = "C:/Users/name/Downloads/MyFiles"
>> > file.names <- dir(path, pattern =".PDF")
>> >
>> > Example output
>> > Output:
>> > "February.PDF"  "January.PDF" "March.PDF"
>> > Desired output
>> > "January.PDF"  "February.PDF" "March.PDF"
>> >
>> > ______________________________________________
>> > [hidden email] 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.
>> Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních
>> partnerů PRECHEZA a.s. jsou zveřejněny na:
>> https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information
>> about processing and protection of business partner’s personal data are
>> available on website:
>> https://www.precheza.cz/en/personal-data-protection-principles/
>> Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou
>> důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení
>> odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any
>> documents attached to it may be confidential and are subject to the legally
>> binding disclaimer: https://www.precheza.cz/en/01-disclaimer/
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Rounding behavior

Jeff Newmiller
In reply to this post by rlderi01
Floating point numbers are approximations in base 2, so any fractions not representing such numbers may round off in unexpected directions.

sprintf( "%22.20f", 0.5 )
sprintf( "%22.20f", 0.575 )

See R FAQ 7.31.

On October 9, 2018 10:14:54 AM PDT, Ryan Derickson <[hidden email]> wrote:

>Hello,
>
>Apologies if this is a simple misunderstanding.
>
>round((.575*100),0) gives 57
>round(57.5,0) gives 58
>
>Why?
>
>Ryan Derickson
>University of Cincinnati
>
>On Tue, Oct 9, 2018, 10:08 AM PIKAL Petr <[hidden email]>
>wrote:
>
>> Hi
>>
>> You could use brute force approach. Just print out "file.names" and
>> estimate ordering vector.
>> In czech locale it is
>>
>> oo <- c(6, 11, 1, 4, 5, 2, 3, 10, 12, 9, 7, 8)
>>
>> In english locale it is different :-)
>>
>> After that
>> file.names[oo]
>>
>> should give you correct order of file names
>>
>> Cheers
>> Petr
>>
>> > -----Original Message-----
>> > From: R-help <[hidden email]> On Behalf Of Ek Esawi
>> > Sent: Tuesday, October 9, 2018 3:44 PM
>> > To: [hidden email]
>> > Subject: [R] Reorder file names read by list.files function
>> >
>> > Hi All--
>> >
>> > I used base R list.file function to read files from a directory.
>The
>> file names are
>> > months (April, August, etc). That's the system reads them in
>> alphabetical order.,
>> > but i want to reordered them in calendar order (January, February,
>> > ...December).. I thought i might be able to do it via RegEx or
>possibly
>> gtools
>> > package, I am wondering if there is an easier way.
>> >
>> > Thanks--EK
>> >
>> > Example
>> > path = "C:/Users/name/Downloads/MyFiles"
>> > file.names <- dir(path, pattern =".PDF")
>> >
>> > Example output
>> > Output:
>> > "February.PDF"  "January.PDF" "March.PDF"
>> > Desired output
>> > "January.PDF"  "February.PDF" "March.PDF"
>> >
>> > ______________________________________________
>> > [hidden email] 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.
>> Osobní údaje: Informace o zpracování a ochraně osobních údajů
>obchodních
>> partnerů PRECHEZA a.s. jsou zveřejněny na:
>> https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information
>> about processing and protection of business partner’s personal data
>are
>> available on website:
>> https://www.precheza.cz/en/personal-data-protection-principles/
>> Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou
>> důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení
>> odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and
>any
>> documents attached to it may be confidential and are subject to the
>legally
>> binding disclaimer: https://www.precheza.cz/en/01-disclaimer/
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] 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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Rounding behavior

Benoit Vaillant
In reply to this post by rlderi01
Hello,

On Tue, Oct 09, 2018 at 01:14:54PM -0400, Ryan Derickson wrote:
> Apologies if this is a simple misunderstanding.

See for example:
https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f

> round((.575*100),0) gives 57
> round(57.5,0) gives 58
>
> Why?

Not R related at all.

$ python
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> round((.575*100),0)
57.0
>>> round(57.5,0)
58.0

Same "issue". :)

You'll need to dig into how numbers are floating numbers are
represented in a finite set.

Cheers,

--
Benoît Vaillant

______________________________________________
[hidden email] 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.

signature.asc (883 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Rounding behavior

rlderi01
I thought it might be a floating issue but didn't see the connection.
Thanks everyone.

On Tue, Oct 9, 2018, 2:00 PM Benoit Vaillant <[hidden email]>
wrote:

> Hello,
>
> On Tue, Oct 09, 2018 at 01:14:54PM -0400, Ryan Derickson wrote:
> > Apologies if this is a simple misunderstanding.
>
> See for example:
>
> https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
>
> > round((.575*100),0) gives 57
> > round(57.5,0) gives 58
> >
> > Why?
>
> Not R related at all.
>
> $ python
> Python 2.7.13 (default, Sep 26 2018, 18:42:22)
> [GCC 6.3.0 20170516] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> round((.575*100),0)
> 57.0
> >>> round(57.5,0)
> 58.0
>
> Same "issue". :)
>
> You'll need to dig into how numbers are floating numbers are
> represented in a finite set.
>
> Cheers,
>
> --
> Benoît Vaillant
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.