0

Pretty simple question. I am trying to sample 20 numbers (with replacement) from the first 10 natural numbers.

This

sample(x = 1:10, size = 0.2*100, replace = TRUE) %>% length

gives 20 (and works), but

sample(x = 1:10, size = (1-0.8)*100, replace = TRUE) %>% length

gives 19.

I can always do

sample(x = 1:10, size = (100 - 0.8*100), replace = TRUE) %>% length

(that works fine and shows 20), but I'm curious why having it in parenthesis doesn't work.

wrahool
  • 1,101
  • 4
  • 18
  • 42

1 Answers1

1

It is because the value is not exactly equal to 20 in size. We can either round or use ceiling

sample(x = 1:10, size = ceiling((1-0.8)*100), replace = TRUE) %>% length
#[1] 20

20 - (1-0.8)*100
#[1] 3.552714e-15
akrun
  • 874,273
  • 37
  • 540
  • 662