[R] ? about sample

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Mar 8 13:44:27 CET 2002

"jimi adams" <imij at columbus.rr.com> writes:

> 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
> while this is not exactly what i am using it for, i need the capabilities to
> deal with zeros (as the probability valuees that i will be using wil vary,
> and sometimes will include zeros), and this seems like the zero's are the
> problem...
> currently i am assigning zero values in my initial probability vector to a
> miniscule value(1E-4)
> is there a more efficient way to deal with this, or another command that
> does a similar thing to only select a single element, where as sample orders
> the entire set according to the prob?

sample (1:4, size=1, prob=c(0,0,1,0))

The default is size=4 and subsetting the result doesn't change that.
Notice that this is sampling without replacement, so what is the
algorithm supposed to do when the first element has been sampled?

   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
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