[R] sampling vectors

Nordlund, Dan NordlDJ at dshs.wa.gov
Tue Oct 4 22:07:24 CEST 2005


Eric,


> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-
> bounces at stat.math.ethz.ch] On Behalf Of Eric Pante
> Sent: Tuesday, October 04, 2005 8:47 AM
> To: Daniel Nordlund
> Cc: r-help at 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  1  3  1  7
> 6
> [24]  4  2 75
>  > vectorSample(ex)
>   [1]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  2  1  1  1  2
> 11
> [24]  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[1] <- tot - sum(v)
  sample(v) #return vector in random order
  }

Dan Nordlund
Bothell, WA




More information about the R-help mailing list