I am trying to create a vector in R using the function rep()
rep()
p <- .9 n <- 100 rep(8,n*(1-p)^2) # expect 8
What causes unexpected behavior?
The reason for this is in the comments on the question. Workaround uses:
rep(8, round(n*(1-p)^2))
Compression of comments. The second argument repmust be an integer. On the help page:, ?as.integerwe know that real numbers are truncated to zero. So,
rep
?as.integer
n*(1-p)^2
transmitted
as.integer(n*(1-p)^2)
which is equal 0.
0