[R] A question to get all possible combinations

jim holtman jholtman at gmail.com
Wed Dec 22 22:29:38 CET 2010


which 6 cells do you want?  do you want one from each column?  You
could use expand.grid:

> x <- expand.grid(1:8, 1:8, 1:8, 1:8, 1:8, 1:8)
> str(x)
'data.frame':   262144 obs. of  6 variables:
 $ Var1: int  1 2 3 4 5 6 7 8 1 2 ...
 $ Var2: int  1 1 1 1 1 1 1 1 2 2 ...
 $ Var3: int  1 1 1 1 1 1 1 1 1 1 ...
 $ Var4: int  1 1 1 1 1 1 1 1 1 1 ...
 $ Var5: int  1 1 1 1 1 1 1 1 1 1 ...
 $ Var6: int  1 1 1 1 1 1 1 1 1 1 ...
 - attr(*, "out.attrs")=List of 2
  ..$ dim     : int  8 8 8 8 8 8
  ..$ dimnames:List of 6
  .. ..$ Var1: chr  "Var1=1" "Var1=2" "Var1=3" "Var1=4" ...
  .. ..$ Var2: chr  "Var2=1" "Var2=2" "Var2=3" "Var2=4" ...
  .. ..$ Var3: chr  "Var3=1" "Var3=2" "Var3=3" "Var3=4" ...
  .. ..$ Var4: chr  "Var4=1" "Var4=2" "Var4=3" "Var4=4" ...
  .. ..$ Var5: chr  "Var5=1" "Var5=2" "Var5=3" "Var5=4" ...
  .. ..$ Var6: chr  "Var6=1" "Var6=2" "Var6=3" "Var6=4" ...
> head(x)
  Var1 Var2 Var3 Var4 Var5 Var6
1    1    1    1    1    1    1
2    2    1    1    1    1    1
3    3    1    1    1    1    1
4    4    1    1    1    1    1
5    5    1    1    1    1    1
6    6    1    1    1    1    1



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?
>
> Thanks,
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org 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.
>
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list