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
 >
 > vectorSample(ex)
  [1]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  
1
[24]  1  6 87
 > vectorSample(ex)
  [1]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  2  1  1  1  1  1  2  
3
[24]  1  4 85
 > vectorSample(ex)
  [1]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1 
19
[24] 16 42 18
 > vectorSample(ex)
  [1]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  2  1  3  5  4  4 
17
[24]  2 53 10
 > vectorSample(ex)
  [1]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  3  
5
[24]  3 11 74
 > vectorSample(ex)
  [1]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  3  3  1  2  
3
[24] 21 23 43
 > vectorSample(ex)
  [1]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  5  
9
[24] 18 20 44
 > vectorSample(ex)
  [1]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  2  1  3  6 
14
[24]  8  6 59


On Oct 4, 2005, at 12:02 PM, Daniel Nordlund wrote:

>> -----Original Message-----
>> From: [EMAIL PROTECTED] 
>> [mailto:[EMAIL PROTECTED]
>> On Behalf Of ecatchpole
>> Sent: Monday, October 03, 2005 11:57 PM
>> To: Eric Pante
>> Cc: r-help@stat.math.ethz.ch
>> Subject: Re: [R] sampling vectors
>>
>> Eric,
>>
>> If you want samples of size 3 from 0:50, with sum==50, this seems to 
>> do
>> the job (with apologies to those who really know how to program in R):
>>
>> tot <- 50
>> ii <- 0
>> aa <- list()
>> for(i in 0:tot){
>>     for(j in 0:(tot-i)){
>>        k <- tot-i-j
>>        ii <- ii+1
>>        aa[[ii]] <- list(i=i,j=j,k=k)
>>     }
>> }
>>
>> aa[sample(ii, 4)] # for a sample of 4.
>>
>> If you want a sample of size n from x[1],...x[N], such that the sample
>> sum is T, then that is much trickier!
>>
>> Ted.
>>
>> On 04/10/05 07:31,  Eric Pante wrote,:
>>> Hello Listers,
>>>
>>> I am trying to sample a vector to create a new one of sample length,
>>> witha  sum equal to the sum of the initial vector:
>>>
>>> initial = 10, 30, 10 (sum=50)
>>> sample example = 5, 35, 10 (sum=50) or 25, 15, 10  (sum=50), etc ...
>>>
>>> My problem is to control the sum, so it stays constant.
>>> Any suggestions would be very helpful !
>>>
>>> Thank you in advance,
>>> Eric
>>>
>
> Eric,
>
> Here is another solution, which allows vectors of different lengths 
> and sums.
>
> 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)
>     }
>   v[1] <- tot - sum(v)
>   v
>   }
>
>  vectorSample( c(10,30,10) )
>
> Hope this helps,
>
> Dan Nordlund
> Bothell, WA
>
>
>
>
Eric Pante
---------------------------------------------------------------
Graduate Student in Marine Biology
Grice Marine Laboratory
205 Fort Johnson Road, Charleston SC 29412
---------------------------------------------------------------

        "On ne force pas la curiosite, on l'eveille ..."
        Daniel Pennac

        [[alternative text/enriched version deleted]]

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to