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)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? Thanks in advance Nicolas
try this: S. <- A[!A%in%S] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: <nicolas.deig at epfl.ch> To: <r-help at stat.math.ethz.ch> Sent: Thursday, January 13, 2005 1:50 PM 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) > > 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? > > 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 >
which(A%in%S==FALSE) Best Matthias> -----Urspr?ngliche Nachricht----- > Von: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] Im Auftrag von > nicolas.deig at epfl.ch > Gesendet: Donnerstag, 13. J?nner 2005 13:51 > An: r-help at stat.math.ethz.ch > Betreff: [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) > > 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? > > 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 >
Hi, On Thu, 13 Jan 2005 nicolas.deig at epfl.ch wrote:> > > A<-c(1:200) > > S<-sample(A,100) > 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?Something like: A[-S] should do, I think. Kev -------------------------------- Ko-Kang Kevin Wang PhD Student Centre for Mathematics and its Applications Building 27, Room 1004 Mathematical Sciences Institute (MSI) Australian National University Canberra, ACT 0200 Australia Homepage: http://wwwmaths.anu.edu.au/~wangk/ Ph (W): +61-2-6125-2431 Ph (H): +61-2-6125-7407 Ph (M): +61-40-451-8301
See ?setdiff. Andy> From: nicolas.deig at epfl.ch > > 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) > > 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? > > 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 > >
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
On Thu, Jan 13, 2005 at 01:50:38PM +0100, nicolas.deig at epfl.ch wrote:> I need to divide the array > > > A<-c(1:200) > > into two subsets at random. Therefore I use the function "sample" in R:I suggest a slightly different approach which is probably faster than using setdif() or %in% and friends: a <- 1:200 i <- sample(1:200, 100) s1 <- a[i] s2 <- a[-i] cu Philipp -- Dr. Philipp Pagel Tel. +49-89-3187-3675 Institute for Bioinformatics / MIPS Fax. +49-89-3187-3585 GSF - National Research Center for Environment and Health Ingolstaedter Landstrasse 1 85764 Neuherberg, Germany http://mips.gsf.de/staff/pagel