Re: [R] sampling vectors

From: ecatchpole <e.catchpole_at_adfa.edu.au>
Date: Tue 04 Oct 2005 - 16:57:02 EST

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
>
> ______________________________________________
> 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

-- 
Dr E.A. Catchpole
Visiting Fellow
Univ of New South Wales at ADFA, Canberra, Australia
and University of Kent, Canterbury, England
- www.ma.adfa.edu.au/~eac
- fax: +61 2 6268 8786		
- ph:  +61 2 6268 8895

______________________________________________
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
Received on Tue Oct 04 16:59:50 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:40:35 EST