0

I am having an issue with the R function rep or maybe its something simpler that I am doing wrong.

    k = ((100)*(1-0.9))
    k # k = 10
    length(rep(0,times = k)) # length of 9 instead of 10
    length(rep(0,times = 10) # length of 10 ! 

This is really weird as the variable k has 10 as value.

Ajmal
  • 137
  • 7

1 Answers1

2

Have a closer look at k. It's not an integer and it's also not really 10.

typeof(k)
# [1] "double"
print(k, digits = 22)
# [1] 9.999999999999998223643

We care about k being integer valued because the times argument of rep() should be an integer. Furthermore, from help(rep):

Non-integer values of times will be truncated towards zero.

So k becomes

trunc(k)
# [1] 9

See the link @Gregor left in the comments for why k is not really 10.

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245