[R] random samples

John Fox jfox at mcmaster.ca
Thu Jan 13 15:02:40 CET 2005


Dear Nicolas,

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of 
> nicolas.deig at epfl.ch
> Sent: Thursday, January 13, 2005 7:51 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] random samples
> 
> hi,
> I am encoutering a very little problem that seemed to be so 
> easy to solve....
> I need to divide the array
> 
> > A<-c(1:200)

Note that A is a vector, not an array, and that you don't need c().

> 
>  into two subsets at random. Therefore I use the function 
> "sample" in R:
> 
> > S<-sample(A,100)
> 
>  for a random sample of size 100. Then I need the values in A 
> that are not selected in S to be put in another array, there 
> is my problem!
> Is there anyway to do this with a function of R or should I 
> do one by myself?
> 

If in your application, as in your example, the elements of A are all
distinct, then setdiff(A, S) will give you what you want. If the elements of
A are not distinct, then you could sample the indices of the elements and
proceed as above, indexing A by the two vectors of indices.

I hope this helps.
 John

> Thanks in advance
> Nicolas
> 
> ______________________________________________
> R-help at 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




More information about the R-help mailing list