1

I am interested to generate all the possible combinations of a vector, for example,

v1 <- c("A", "Aa", "B")

and all the possible combinations will be

"A-Aa" "Aa-A" "B-A"  "B-Aa" "A-B"  "Aa-B"

I saw a post- How can I generate all the possible combinations of a vector, but it does not give correct results for v1. The same thing happens to v1 <- c("Testis_NOA_ID","Testis_NOA_IDSt") which returns "Testis_NOA_ID-Testis_NOA_IDSt" only but I am expecting "Testis_NOA_IDSt-Testis_NOA_ID" also.

I checked some other vectors such as v1 <- c("a,"b","c) or v1 <- c("normal", "cancer"), which it gives correct results. The problem comes when the vector content is repeating like v1 <- c("Testis_NOA_ID","Testis_NOA_IDSt"). How to fix it.

AwaitedOne
  • 992
  • 3
  • 19
  • 42
  • 4
    You need `res <- outer(v1, v1, FUN = paste, sep="-"); res[row(res) != col(res)]` – akrun Feb 18 '18 at 15:33
  • Great!!, Can u please tell how it works? – AwaitedOne Feb 18 '18 at 15:37
  • 1
    As I mentioned in the other post, `outer` gives all the combinations, by specifying the `FUN` as `paste`, you get the concatenated combinations of the vector elements in a `matrix`. Based on your expected output, the same element combinations are removed which happens to be on the diagonal and that elements are removed with `row(res) != col(res)` – akrun Feb 18 '18 at 15:46

0 Answers0