Re: [R] random section of samples based on group membership

From: Carlos J. Gil Bellosta <cgb_at_datanalytics.com>
Date: Tue 25 Jul 2006 - 01:31:52 EST

Dear Wade,

Say that your groups are

groups <- sort(sample(1:10, 100, replace = TRUE))

Create a dummy

rows <- 1:length(groups)

Then

tapply( rows, groups, function(x) sample(x, 1))

does the trick to select the row numbers you need for your sampling.

Sincerely,

Carlos J. Gil Bellosta
http://www.datanalytics.com
http://www.data-mining-blog.com

Quoting Wade Wall <wade.wall@gmail.com>:

> Hi all,
>
> I have a matrix of 474 rows (samples) with 565 columns (variables).
> each of the 474 samples belong to one of 120 groups, with the
> groupings as a column in the above matrix. For example, the group
> column would be:
>
> 1
> 1
> 1
> 2
> 2
> 2
> .
> .
> .
> 120
> 120
>
> I want to randomly select one from each group. Not all the groups
> have the same number of samples, some have 4, some 3 etc. Is there a
> function to do this, or would I need to write a looping statement to
> look at each successive group?
>
> I basically want to combine the randomly selected samples from the 120
> groups into a new matrix in order to perform a cluster analysis.
>
> Thanks,
> Wade
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>



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 and provide commented, minimal, self-contained, reproducible code. Received on Tue Jul 25 01:58:55 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 25 Jul 2006 - 02:20:54 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.