[R] using sample() for a vector of length 1

2010-07-22 Thread Jon BR
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

Re: [R] using sample() for a vector of length 1

2010-07-22 Thread Henrique Dallazuanna
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 jonsle...@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

Re: [R] using sample() for a vector of length 1

2010-07-22 Thread Hadley Wickham
Did you look at the examples in sample? # sample()'s surprise -- example x - 1:10 sample(x[x 8]) # length 2 sample(x[x 9]) # oops -- length 10! sample(x[x 10]) # length 0 ## For R = 2.11.0 only resample - function(x, ...) x[sample.int(length(x), ...)] resample(x[x 8]) # length

Re: [R] using sample() for a vector of length 1

2010-07-22 Thread Jonathan
I see.. Thanks! On Thu, Jul 22, 2010 at 4:39 PM, Hadley Wickham had...@rice.edu wrote: Did you look at the examples in sample? # sample()'s surprise -- example x - 1:10    sample(x[x  8]) # length 2    sample(x[x  9]) # oops -- length 10!    sample(x[x 10]) # length 0 ## For R =

Re: [R] using sample() for a vector of length 1

2010-07-22 Thread Ted Harding
And, surely, regardless of R version: resamp - function(x,...){if(length(x)==1) x else sample(x,...)} resamp((1:10),10) # [1] 8 2 10 6 5 4 3 7 9 1 resamp((1:10),1) # [1] 7 resamp((1:10),1) # [1] 4 resamp((1:10),1) # [1] 10 resamp(10,1) # [1] 10 resamp(10,1) #