Try this:

x <- 10
sample(x, 1, prob = c(rep(0, x - 1), 1))


On Thu, Jul 22, 2010 at 5:31 PM, Jon BR <jonsleepy@gmail.com> wrote:

> Hi All,
>    I'm trying to use the "sample" function within a loop where the
> vector being sampled from (the first argument in the function) will
> vary in length and composition.  When the vector is down in size to
> containing only one element, I run into the "undesired behaviour"
> acknowledged in the ?sample help file.  I don't want sample(10,1) to
> return a number from within 1:10, but rather I'd just want it to
> return 10 every time.
>
> Example):
>
>
> Actual:
> > sample(10,1)
> [1] 2
> > sample(10,1)
> [1] 9
> > sample(10,1)
> [1] 4
>
>
> Desired:
> > sample(10,1)
> [1] 10
> > sample(10,1)
> [1] 10
> > sample(10,1)
> [1] 10
>
>
> Perhaps sample is not the appropriate function.  I dunno.  Any thoughts?
>
> Regards,
> Jonathan
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

	[[alternative HTML version deleted]]

