ratio[i]
[1] 0.9
length(sample(c(1,2,3,4,5,6,7,8,9),2000*ratio[i],replace=T))
[1] 1800
length(sample(c(1,2,3,4,5,6,7,8,9),2000*(1-ratio[i]),replace=T))
[1] 199
It looks like R is doing the calculation incorrectly. I tried a few more number, sometimes it is correct, but sometimes it is not. So I did the following.
space<-matrix(nrow=10000,ncol=2)
for (i in 1:10000){
#expected
space[i,1]<-20000*(1-i/10000)
#actual
space[i,2]<-length(sample(1,20000*(1-i/10000),replace=T))
}
plot(space[,1]-space[,2])
It appears that this problem is not limited to a few numbers.