[R] how to subsample all possible combinations of n species taken 1:n at a time?

David Katz david at davidkatzconsulting.com
Tue Apr 7 01:17:41 CEST 2009


If I understand your problem properly, you could just note that selecting 1:n
of n objects is the same as deciding separately whether each one is included
or not. (exclude the case where none are selected).

Take 1000 of these and you are there- except some are duplicates - so
generate extras and eliminate the duplicates, discard the extras.

Something like this (not tested):

p <- 2^(n-1) / (2^n - 1) #all combinations have equal probability - removing
rows with all zeros
result <- matrix(0,1200*n,nrow=1200) #plenty of extras for duplicates
for(i in 1:1200) result[i,] <- rbinom(n,1,p)
result <- subset(result,apply(result,1,sum) > 0) #cases which have at least
1 species
result <- unique(result)[1:1000,]

Might be interesting to see the effect of varying p on the rest of your
analysis.

Further memory might be saved by using sparse matrices - see the Matrix
package.

David Katz
www.davidkatzconsulting.com


jasper slingsby wrote:
> 
> Hello
> 
> I apologise for the length of this entry but please bear with me.
> 
> In short:
> I need a way of subsampling communities from all possible communities of n
> taxa taken 1:n at a time without having to calculate all possible
> combinations (because this gives me a memory error - using 
> combn() or expand.grid() at least). Does anyone know of a function? Or can
> you help me edit the 
> combn
> or 
> expand.grid 
> functions to generate subsamples?
> 
> 

-- 
View this message in context: http://www.nabble.com/how-to-subsample-all-possible-combinations-of-n-species-taken-1%3An-at-a-time--tp22911399p22919388.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list