user3327637
user3327637

Reputation: 109

how to perform a combination procedure on a vector such that i have the original vector as the first row

I have this vector

 b=c(5,8,9)

I want to perform a combination on b selecting 2 items at a time such that i have the original elements of b as my first row to get

        [,1] [,2] [,3]
[1,]    5     8    9
[2,]    8     9    5

I tried combn(b, 2) and it gives me this

        [,1] [,2] [,3]
[1,]    5     5    8
[2,]    8     9    9

Can i get help to achieve my desired result?

Upvotes: 0

Views: 75

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48211

Since the second row of your desired result is not uniquely defined, there is no need for any sophisticated tools:

b <- 1:10
rbind(b, c(b[-1], b[1]))
#   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# b    1    2    3    4    5    6    7    8    9    10
#      2    3    4    5    6    7    8    9   10     1

In this case I only "shift" b by one position in the second row, which indeed results in a permutation. I'm assuming that the elements of b don't repeat.

Upvotes: 1

Related Questions