Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate list of all possible combinations of elements of vector

Tags:

r

combinations

I am trying to generate all possible combinations of 0 and 1's in a vector of length 14. Is there an easy way of getting that output as a list of vectors, or even better, a dataframe?

To demonstrate better what I am looking for, let's suppose that I only want a vector of length 3. I would like to be able to generate the following:

 (1,1,1), (0,0,0), (1,1,0), (1,0,0), (1,0,1), (0,1,0), (0,1,1), (0,0,0) 
like image 979
Mayou Avatar asked Sep 09 '13 19:09

Mayou


People also ask

How do you generate all possible combinations of one list?

Add a Custom Column to and name it List1. Enter the formula =List1. Expand out the new List1 column and then Close & Load the query to a table. The table will have all the combinations of items from both lists and we saved on making a custom column in List1 and avoided using a merge query altogether!

How do you get all the combinations of a vector in R?

To create combination of multiple vectors, we can use expand. grid function. For example, if we have six vectors say x, y, z, a, b, and c then the combination of vectors can be created by using the command expand. grid(x,y,z,a,b,c).

How do you get combinations in R?

combn() method in R language belonging to this package is used to generate all combinations of the elements of x taken m at a time. If x is a positive integer, returns all combinations of the elements of seq(x) taken m at a time. Syntax: combn(x, m, fun=NULL, simplify=TRUE, …)

What does Combn function in R do?

The combn() function in R is used to return the combination of the elements of a given argument x taken m at a time.


2 Answers

You're looking for expand.grid.

expand.grid(0:1, 0:1, 0:1) 

Or, for the long case:

n <- 14 l <- rep(list(0:1), n)  expand.grid(l) 
like image 171
Justin Avatar answered Sep 20 '22 06:09

Justin


tidyr has a couple of options similar to expand.grid().

tidyr::crossing() returns a tibble and does not convert strings to factors (though you could do expand.grid(..., stringsAsFactors = F)).

library(tidyr)  crossing(var1 = 0:1, var2 = 0:1, var3 = 0:1) # A tibble: 8 x 3    var1  var2  var3   <int> <int> <int> 1     0     0     0 2     0     0     1 3     0     1     0 4     0     1     1 5     1     0     0 6     1     0     1 7     1     1     0 8     1     1     1 

tidyr::expand() can give both combinations of only values that appear in the data, like this:

expand(mtcars, nesting(vs, cyl)) # A tibble: 5 x 2      vs   cyl   <dbl> <dbl> 1     0     4 2     0     6 3     0     8 4     1     4 5     1     6 

or all possible combinations of two variables, even if there isn't an observation with those specific values in the data in the data, like this:

expand(mtcars, vs, cyl) # A tibble: 6 x 2      vs   cyl   <dbl> <dbl> 1     0     4 2     0     6 3     0     8 4     1     4 5     1     6 6     1     8 

(You can see that there were no observations in the original data where vs == 1 & cyl == 8)

tidyr::complete() can also be used similar to expand.grid(). This is an example from the docs:

df <- dplyr::tibble(   group = c(1:2, 1),   item_id = c(1:2, 2),   item_name = c("a", "b", "b"),   value1 = 1:3,   value2 = 4:6 ) df %>% complete(group, nesting(item_id, item_name))  # A tibble: 4 x 5   group item_id item_name value1 value2   <dbl>   <dbl> <chr>      <int>  <int> 1     1       1 a              1      4 2     1       2 b              3      6 3     2       1 a             NA     NA 4     2       2 b              2      5 

This gives all possible combinations of item_id and item_name for each group - it creates a line for group=2 item_id=1 and item_name=a.

like image 22
sbha Avatar answered Sep 20 '22 06:09

sbha