Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying a function to every combination of elements in a vector

Tags:

function

r

apply

I would like to apply a certain (custom) function to all combinations of an array. I think its best to explain with an example:

Matrix 1 :

A B C
1 2 3

Matrix 2 :

A B C  
4 5 6

I would like to do the following: obtain all the combinations of Matrix two and apply a function to each as follows:

Matrix 3 :

AB  AC  BC  CB  CA  BA  
4/2 4/3 5/3 6/2 6/1 5/1  

Where the function applied to Matrix 3 is the corresponding element of Matrix 2 (represented by the first letter in each column of Matrix 3)/the corresponding element of Matrix 2 (represented by the second letter in each column in Matrix 3).

Please let me know if anything is unclear, I feel that I may not have explained perfectly.

Any help would be greatly appreciated!

Thanks

Mike

like image 427
Mike Avatar asked Oct 07 '13 09:10

Mike


People also ask

Which function creates matrices that allow every combination of values within two input vectors?

Generate All Combinations of Vectors Using the combvec Function. This example shows how to generate a matrix that contains all combinations of two matrices, a1 and a2 . Create the two input matrices, a1 and a2 . Then call the combvec function to generate all possible combinations.

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 generate all possible combinations of one list?

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!

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

The result is not exactly in the format you asked for, but you can use outer to create a matrix of results from your two input vectors :

x <- c(A=1,B=2,C=3)
y <- c(A=4,B=5,C=6)
outer(x,y, FUN="/")

Will give :

     A   B         C
A 0.25 0.2 0.1666667
B 0.50 0.4 0.3333333
C 0.75 0.6 0.5000000

If you really want a vector as result, you can use :

m <- outer(x,y, FUN="/")
v <- as.vector(m)
names(v) <- as.vector(outer(names(x),names(y),FUN="paste0"))

And then get :

       AA        BA        CA        AB        BB        CB        AC 
0.2500000 0.5000000 0.7500000 0.2000000 0.4000000 0.6000000 0.1666667 
       BC        CC 
0.3333333 0.5000000 
like image 150
juba Avatar answered Sep 28 '22 22:09

juba


This can be done simply if you install gtools and use the permutations function.

require(gtools)
M1 <- list(A=1, B=2, C=3)
M2 <- list(A=4, B=5, C=6)

perms <- t(permutations(3, 2, 1:3))

comboList <- list()
for (i in 1:ncol(perms)) {
    nameString <- paste0(names(M2)[perms[1,i]], names(M1)[perms[2,i]])
    comboList[[i]] <- mapply("/", M2[[perms[,i][1]]], M1[[perms[,i][2]]])
}

The mapply function is a pretty magical built in R function. It's worth while getting the know the entire family of *apply functions.

The output is in comboList, which is as follows:

> comboList
$AB
[1] 2

$AC
[1] 1.333333

$BA
[1] 5

$BC
[1] 1.666667

$CA
[1] 6

$CB
[1] 3
like image 38
ricardo Avatar answered Sep 28 '22 23:09

ricardo