Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create all possible permutations of two vectors in R [duplicate]

Tags:

r

I have two vectors like this:

f1=c('a','b','c','d')
e1=c('e','f','g')

There is 4^3 different permutations of them. I need to create all of possible permutations of them in R softeware.for example;

(1):
a e
a f
a g
(2):
a e
a f
b g
...

Moreover, my real data are very huge and I need speed codes.

like image 701
user2877990 Avatar asked Nov 22 '13 16:11

user2877990


1 Answers

It sounds like you are looking for expand.grid.

> expand.grid(f1, e1)
   Var1 Var2
1     a    e
2     b    e
3     c    e
4     d    e
5     a    f
6     b    f
7     c    f
8     d    f
9     a    g
10    b    g
11    c    g
12    d    g

I don't know what "speed codes" are, so I'm not sure I can help from that aspect.

like image 185
A5C1D2H2I1M1N2O1R2T1 Avatar answered Nov 02 '22 21:11

A5C1D2H2I1M1N2O1R2T1