A question to get all possible combinations
Dear Ron,
On Wed, Dec 22, 2010 at 1:19 PM, Ron Michael <ron_michael70 at yahoo.com> wrote:
Let say, I have a matrix with 8 rows and 6 columns:
?df1 ?<- matrix(NA, 8, 4) df1
? ? ?[,1] [,2] [,3] [,4] [1,] ? NA ? NA ? NA ? NA [2,] ? NA ? NA ? NA ? NA [3,] ? NA ? NA ? NA ? NA [4,] ? NA ? NA ? NA ? NA [5,] ? NA ? NA ? NA ? NA [6,] ? NA ? NA ? NA ? NA [7,] ? NA ? NA ? NA ? NA [8,] ? NA ? NA ? NA ? NA Now I want to get **all possible** ways to fetch 6 cells at a time. Is there any function to do that?
A matrix is just a vector, so each possible sample of size 6 from df1 corresponds to a sample of size 6 from the vector 1:32. There are many, many ways to get all of those (there are choose(32,6) such samples). The way I do it is library(prob) urnsamples(1:32, size = 6) and you can find lots of other, faster ways. If you store all of those in a data frame A, say, then you can get all possible samples from df1 with something like apply(A, 2, function(x) df1[x])) The good news is that the result will also be a matrix. Each row will be a possible sample of size 6 from df1. Hope this helps, Jay P.S. Note that urnsamples by default is replace = FALSE, ordered = FALSE, which is how I got away with the short command above. __________________________ G. Jay Kerns, Ph.D. Youngstown State University http://people.ysu.edu/~gkerns/