Unexpected behavior of rep () in R

I am trying to create a vector in R using the function rep()

p <- .9
n <- 100
rep(8,n*(1-p)^2) # expect 8

What causes unexpected behavior?

+4
source share
2 answers

The reason for this is in the comments on the question. Workaround uses:

rep(8, round(n*(1-p)^2))
0
source

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,

n*(1-p)^2

transmitted

as.integer(n*(1-p)^2)

which is equal 0.

0
source

All Articles