Giter Club home page Giter Club logo

Comments (7)

Stefan2015-5 avatar Stefan2015-5 commented on July 22, 2024 1

I must say it is embarrassing that this bug happened in the first place but it is more embarrassing that it is not fixed. I nearly had a report going out with "NaN%" in there. Sorry, if this comes across angry, but it makes me sad when you can't rely on these easy things.

my.percent<- function(x) {
if(length(x)==1) if(x==0) return(paste0(0,"%") )
return(percent(x) )
}

from scales.

BrianDiggs avatar BrianDiggs commented on July 22, 2024

The problem ultimately arises because plyr:::round_any(0, 0) return NaN. (See https://github.com/hadley/plyr/blob/master/R/round-any.r#L28). I'm not sure what round_any should return when accuracy == 0; I'm thinking that it should just return x unaltered. Or a vector of 0 the same length as x.

# not real code because we are talking about a function in another package.
plyr:::round_any.numeric
function (x, accuracy, f = round) 
{
    if(accuracy == 0) {
        x
    } else {
        f(x/accuracy) * accuracy
    }
}

from scales.

dougmitarotonda avatar dougmitarotonda commented on July 22, 2024

I think that is a good solution. percent similarly does not work for negative numbers and I submitted a pull request for that here: #40. It would be great to see both these changes made so that percent works on all real numbers.

from scales.

hadley avatar hadley commented on July 22, 2024

@BrianDiggs I suspect there's a better rounding algorithm that will also work with small x. Hopefully twitter can help: http://twitter.com/hadleywickham/status/608679186597695488

from scales.

mvpp avatar mvpp commented on July 22, 2024

Any update on this?
The weird part is that it returns "0%" properly when passed in a vector with at least one no-zero value:

percent(c(0,2))
[1] "0%" "200%"
percent(0)
[1] "NaN%"
percent(c(0,0))
[1] "NaN%" "NaN%"

from scales.

krlmlr avatar krlmlr commented on July 22, 2024

IMO, the precision() function (which is used only by percent()) should be improved:

> precision(c(0.1, 100))
[1] 10
> precision(c(-0.1, 100))
[1] 100
> precision(0)
[1] 0

precision(0) should return 1 (this would fix the issue here), but for the other two I'm not sure.

from scales.

Braonan avatar Braonan commented on July 22, 2024

Could use mvpp's observation for a fix by creating a vector with dummy value and using only first numeric e.g.,
percent(c(0,1))[1]
bit of a dirty fix; but simple and works

from scales.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.