Skip to content

? about sample

3 messages · jimi adams, Peter Dalgaard, Thomas Lumley

#
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?

if it helps, i am using R 1.4.0 on win-me
thanks

jimi adams
Department of Sociology
The Ohio State University
300 Bricker Hall
190 N Oval Mall
Columbus, OH 43210-1353
614-688-4261

our mind has a remarkable ability to think of contents as being independent
of the act of thinking
                                            -georg simmel


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
"jimi adams" <imij at columbus.rr.com> writes:
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?
#
On Fri, 8 Mar 2002, jimi adams wrote:

            
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
means.

	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._