0

I have the following data set.

    long      lat        hex
  1: -74.25121 40.49963   1
  2: -74.24428 40.49920   2
  3: -74.23463 40.50110   3
  4: -74.22234 40.50257   4
  5: -74.24913 40.50574   5

I want all the combinations of the column "hex", order doesn't matter (ex: 1-2,1-3,1-4,4-1,3-1 . . . ). How would I go about approaching this

LoF10
  • 1,907
  • 1
  • 23
  • 64

1 Answers1

0

It sounds like you want permutations, not combinations (both 1-3 and 3-1 are in your example).

library(gtools)
hex_perm <- permutations(n = 5, r = 2, df$hex)
apply(hex_perm, 1, paste0, collapse = "-")

Results in:

 [1] "1-2" "1-3" "1-4" "1-5" "2-1" "2-3" "2-4" "2-5" "3-1" "3-2" "3-4" "3-5" "4-1" "4-2" "4-3" "4-5" "5-1" "5-2" "5-3" "5-4"
Eric Watt
  • 3,180
  • 9
  • 21