ohioihoih
ohioihoih

Reputation: 1

How can I create permutation without any library?

There are n numbers; 1 to n. I want to make permutations of length m with these n numbers. For example, when n is 3 and m is 2, the result will be like this:

list(c(1,2), c(1,3), c(2,1), c(2,3), c(3,1), c(3,2))

I don't care of the order of the result list.

Upvotes: 0

Views: 42

Answers (1)

www
www

Reputation: 39154

I think this could be what you want. a3 is the final output.

a1 <- t(combn(3, 2))
a2 <- a1[, c(2, 1)]
a3 <- rbind(a1, a2)
a3
     [,1] [,2]
[1,]    1    2
[2,]    1    3
[3,]    2    3
[4,]    2    1
[5,]    3    1
[6,]    3    2

Upvotes: 1

Related Questions