# Re: [R] sampling vectors

From: Nordlund, Dan <NordlDJ_at_dshs.wa.gov>
Date: Wed 05 Oct 2005 - 06:07:24 EST

Eric,

> -----Original Message-----
> From: r-help-bounces@stat.math.ethz.ch [mailto:r-help-
> bounces@stat.math.ethz.ch] On Behalf Of Eric Pante
> Sent: Tuesday, October 04, 2005 8:47 AM
> To: Daniel Nordlund
> Cc: r-help@stat.math.ethz.ch
> Subject: Re: [R] sampling vectors
>
> Hi Dan,
>
> I just tried your code with long vectors, and the sampling stops to be
> random. Do you see any reason why this is ?
>
> examples:
>
> ex = c(30,13,9,8,7,7,7,6,6,5,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1)
>
> > vectorSample(ex)
>  1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 7
> 6
>  4 2 75
> > vectorSample(ex)
>  1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 2
> 11
>  7 8 67
> >

Eric,

Probably because I failed to provide a complete solution. I had a couple questions while I was writing this that I didn't get clarified. One, the code needs to be slightly modified if you want to allow vector elements to be zero. I required each element to be >= 1. Two, I did not reorder the elements in the vector. Larger counts are more likely early in the process than late in the process (since the partial sum is approaching the original total). I just placed the counts in the vector result in reverse order of when they were obtained.

The fix to the "randomness" is simple. Just return sample(v) instead of v as the function result (see below). If you want to allow zero elements I think you can just change the lower limit on the sampling to be 0 rather than 1 (not thoroughly tested)

vectorSample <- function(vec) {
tot<-sum(vec)
Len<-length(vec)
v <-rep(0,Len)
for(i in Len:2) {
UL <- tot - sum(v) - i + 1
v[i]<-sample(1:UL,1)

```    #change preceding statement to
#    v[i]<-sample(0:UL,1)
#if you want to allow zeros
```

}
v <- tot - sum(v)
sample(v) #return vector in random order   }

Dan Nordlund
Bothell, WA

R-help@stat.math.ethz.ch mailing list