Unique Combination of All Elements from Two (Or More) Vectors

Pasting two vectors with combinations of all vectors' elements

You can use this, but there may be a simpler solution :

R> apply(expand.grid(vars, vis), 1, paste, collapse=".")
[1] "SR.1" "PL.1" "SR.2" "PL.2" "SR.3" "PL.3"

expand.grid gives back a data.frame which when used with apply, apply will convert it to a matrix. This is just unnecessary (and inefficient on large data). outer gives a matrix and also takes function argument. It'll be much efficient on huge data as well.

Using outer:

as.vector(outer(vars, vis, paste, sep="."))
# [1] "SR.1" "PL.1" "SR.2" "PL.2" "SR.3" "PL.3"

Generate all unique combinations from a vector with repeating elements

Use combn() with lapply() should do the trick.

x <- c('red', 'blue', 'green', 'red', 'green', 'red')

lapply(1:3, function(y) combn(x, y))

# [[1]]
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] "red" "blue" "green" "red" "green" "red"

# [[2]]
# [,1] [,2] [,3] [,4] [,5] [,6] ...
# [1,] "red" "red" "red" "red" "red" "blue" ...
# [2,] "blue" "green" "red" "green" "red" "green" ...

# [[3]]
# [,1] [,2] [,3] [,4] [,5] [,6] ...
# [1,] "red" "red" "red" "red" "red" "red" ...
# [2,] "blue" "blue" "blue" "blue" "green" "green" ...
# [3,] "green" "red" "green" "red" "red" "green" ...

All unique combinations

lapply(cc, function(y)
y[,!duplicated(apply(y, 2, paste, collapse="."))]
)

[[1]]
[1] "red" "blue" "green"

[[2]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] "red" "red" "red" "blue" "blue" "green" "green"
[2,] "blue" "green" "red" "green" "red" "red" "green"

[[3]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] ...
[1,] "red" "red" "red" "red" "red" "red" "blue" ...
[2,] "blue" "blue" "green" "green" "red" "red" "green" ...
[3,] "green" "red" "red" "green" "green" "red" "red" ...

Although strictly speaking those aren't all unique combinations, as some of them are permutations of each other.

Properly unique combinations

lapply(cc, function(y)
y[,!duplicated(apply(y, 2, function(z) paste(sort(z), collapse=".")))]
)

# [[1]]
# [1] "red" "blue" "green"

# [[2]]
# [,1] [,2] [,3] [,4] [,5]
# [1,] "red" "red" "red" "blue" "green"
# [2,] "blue" "green" "red" "green" "green"

# [[3]]
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] "red" "red" "red" "red" "red" "blue"
# [2,] "blue" "blue" "green" "green" "red" "green"
# [3,] "green" "red" "red" "green" "red" "green"

All Possible Pairs between Two Vectors in R Without Replacement

It seems a permutation problem, which might be solved like below

> library(pracma)

> paste0(v1, t(perms(v2)))
[1] "AZ" "BY" "CX" "AZ" "BX" "CY" "AY" "BZ" "CX" "AY" "BX" "CZ" "AX" "BY" "CZ"
[16] "AX" "BZ" "CY"

or

out <- data.frame(
Var1 = v1,
Var2 = c(t(perms(v2))),
Match = ceiling(seq(factorial(length(v2)) * length(v2)) / length(v1))
)

which gives

> out
Var1 Var2 Match
1 A Z 1
2 B Y 1
3 C X 1
4 A Z 2
5 B X 2
6 C Y 2
7 A Y 3
8 B Z 3
9 C X 3
10 A Y 4
11 B X 4
12 C Z 4
13 A X 5
14 B Y 5
15 C Z 5
16 A X 6
17 B Z 6
18 C Y 6

permutations and combinations of [1, 2, 3, ..., n] elements for each of two vectors in R

Here is a method that results in a nested list:

# test vectors
vec1 <- 1:3
vec2 <- 4:6
# create all combinations of vector elements by length as nested list
comboList1 <- lapply(1:length(vec1), function(i) combn(vec1, i, simplify=FALSE))
comboList2 <- lapply(1:length(vec2), function(i) combn(vec2, i, simplify=FALSE))

# get fairly nested list of the Cartesian product of these lists
rapply(comboList1, function(i) rapply(comboList2, function(j) list(i, j),
how="list"), how="list")

As a cleaner reading list, you can use unlist as follows:

# use unlist the results of combn to simplify results
comboList1 <- unlist(comboList1, recursive=FALSE)
comboList2 <- unlist(comboList2, recursive=FALSE)
# now use lapply:
unlist(lapply(vec1, function(i) lapply(vec2, function(j) return(list(i, j)))), recursive=FALSE)

The final use of unlist flattens out the list, resulting in a two-level list where the second level is the comparison of vectors. This is as simplified as possible.

How to create a unique list of combinations from multiple vectors?

I make some modifications to your input data to achieve the goal you wanted.

Data

toppings <- c("Pepperoni", "Canadian Bacon", "Sausage", "Italian Sausage")
crust <- data.frame(crust = c("Thick", "Thin", "Cracker"))

library(arrangements)
topping_combo <- as.data.frame(combinations(toppings, k = 2, n = length(toppings)))
colnames(topping_combo) <- c("topping_1", "topping_2")

So now we have two dataframes: crust with only one column and three rows, one for each type of crust; topping_combo is a 6x2 dataframe which contains all the possible 2-combinations of toppings.

Code

The following code offers one possible solution to create a dataframe in which each row is a type of pizza, as you requested. We use the function crossing from tidyr package.

library(tidyr)
crossing(crust, topping_combo)

Output

     crust      topping_1       topping_2
1 Thick Pepperoni Canadian Bacon
2 Thick Pepperoni Sausage
3 Thick Pepperoni Italian Sausage
4 Thick Canadian Bacon Sausage
5 Thick Canadian Bacon Italian Sausage
6 Thick Sausage Italian Sausage
7 Thin Pepperoni Canadian Bacon
8 Thin Pepperoni Sausage
9 Thin Pepperoni Italian Sausage
10 Thin Canadian Bacon Sausage
11 Thin Canadian Bacon Italian Sausage
12 Thin Sausage Italian Sausage
13 Cracker Pepperoni Canadian Bacon
14 Cracker Pepperoni Sausage
15 Cracker Pepperoni Italian Sausage
16 Cracker Canadian Bacon Sausage
17 Cracker Canadian Bacon Italian Sausage
18 Cracker Sausage Italian Sausage

Unique element pairing between two vectors maximizing the overall sum

Well, I eventually found the solution based on the Hungarian algorithm implemented in the solve_LSAP function of the clue R package. To have it working, transform your df in a matrix like so:

df = matrix(sapply(df$score, function(x) x), nrow=length(unique(df$Var1)), ncol=length(unique(df$Var2)), dimnames = list(unique(df$Var1), unique(df$Var2)))

and apply the function

df.res = solve_LSAP(df, maximum = T)
> df.res
Optimal assignment:
1 => 2, 2 => 3, 3 => 1

and then get back the actual nodes or names

df.res = cbind(rownames(df), colnames(df)[df.res])
> df.res
[,1] [,2]
[1,] "A" "C"
[2,] "B" "D"
[3,] "C" "A"
>

Tadaaaaam!

R: all possible combinations from a vector of elements with 2 possible conditions (+/-)

markers <- LETTERS[1:5]

test <- expand.grid(lapply(seq(markers), function(x) c("+","-")),stringsAsFactors=FALSE)

> test
Var1 Var2 Var3 Var4 Var5
1 + + + + +
2 - + + + +
3 + - + + +
4 - - + + +
....


apply(test,1,function(x){paste0(markers,x,collapse = "/")})


[1] "A+/B+/C+/D+/E+" "A-/B+/C+/D+/E+" "A+/B-/C+/D+/E+" "A-/B-/C+/D+/E+" "A+/B+/C-/D+/E+" "A-/B+/C-/D+/E+" "A+/B-/C-/D+/E+"
[8] "A-/B-/C-/D+/E+" "A+/B+/C+/D-/E+" "A-/B+/C+/D-/E+" "A+/B-/C+/D-/E+" "A-/B-/C+/D-/E+" "A+/B+/C-/D-/E+" "A-/B+/C-/D-/E+"
[15] "A+/B-/C-/D-/E+" "A-/B-/C-/D-/E+" "A+/B+/C+/D+/E-" "A-/B+/C+/D+/E-" "A+/B-/C+/D+/E-" "A-/B-/C+/D+/E-" "A+/B+/C-/D+/E-"
[22] "A-/B+/C-/D+/E-" "A+/B-/C-/D+/E-" "A-/B-/C-/D+/E-" "A+/B+/C+/D-/E-" "A-/B+/C+/D-/E-" "A+/B-/C+/D-/E-" "A-/B-/C+/D-/E-"
[29] "A+/B+/C-/D-/E-" "A-/B+/C-/D-/E-" "A+/B-/C-/D-/E-" "A-/B-/C-/D-/E-"

Find all uniq combinations in R

Update

If you want a function, try below

> f <- function(genes, k) data.frame(t(combn(genes, k)))

> f(genes, 2)
X1 X2
1 A B
2 A C
3 B C

> f(genes, 3)
X1 X2 X3
1 A B C

Do you mean combn?

> genes <- c("A", "B", "C")

> unlist(sapply(2:3, function(k) combn(genes, k, paste0, collapse = "+")))
[1] "A+B" "A+C" "B+C" "A+B+C"


Related Topics



Leave a reply



Submit