[R] random section of samples based on group membership

Mike Nielsen mr.blacksheep at gmail.com
Mon Jul 24 17:52:04 CEST 2006


Well, how you do it might be a matter of taste with respect to how you
want the results.

You could try using "by" with "sample"

by(x,x[,3],function(y){y[sample(nrow(y),1),]})

This will return a list with one list element for each sample group.
You can the combine the list back into a matrix.

That's my naive solution; no doubt there will be half a dozen better
ways to go about it.

Also, some of the clustering functions I have seen will sample for you.


On 7/24/06, Wade Wall <wade.wall at gmail.com> wrote:
> 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 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
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Regards,

Mike Nielsen



More information about the R-help mailing list