Search code examples
rargumentsrep

Why does times argument in rep function not behaving correctly


The answer to this question maybe very simple but I cannot think of obvious reasons for this.

Why does rep(1,40*(1-0.8)) result in vector of length 7 instead of 8? Anyone please.


Solution

  • It's due to floating point precision:

    print(40*(1-0.8), digits = 20)
    ## [1] 7.9999999999999982
    

    round can be used:

    rep(1, round(40*(1-0.8)))
    ## [1] 1 1 1 1 1 1 1 1