[R] ? about sample

Thomas Lumley tlumley at u.washington.edu
Fri Mar 8 18:05:16 CET 2002

On Fri, 8 Mar 2002, jimi adams wrote:

> i am trying to use the
> sample command and have one question about it:
> i am getting the error
> Error in sample(length(x), size, replace, prob) :
>         insufficient positive probabilities
> when i use something like:
> sample (1:4, prob=c(0,0,1,0))[1]
> i was expecting that to return a 3 every time

It sounds like you want to add replace=TRUE, eg

R> sample(1:4,prob=c(0,0,1,0),replace=TRUE)
[1] 3 3 3 3

You were trying to sample four numbers without replacement from a set
effectively containing only one number.  That's what the error message


r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list