1

I have a small example like the following:

df1 = data.frame(Id1=c(1,2,3))

I want to obtain the list of all combinations with replacement which would look like this:

enter image description here

So far I have seen the following functions which produces some parts of the above table:

a) combn function

t(combn(df1$Id1,2)) 

# Does not creates rows 1,4 and 5 in the above image

b) expand.grid function

expand.grid(df1$Id1,df1$Id1) 

# Duplicates rows 2,3 and 5. In my case the combination 1,2 and 2,1 
#are the same. Hence I do not need both of them at the same time.

c) CJ function (from data.table)

#install.packages("data.table")
CJ(df1$Id1,df1$Id1)

#Same problem as the previous function

For your reference, I know that the in python I could do the same using the itertools package (link here: https://www.hackerrank.com/challenges/itertools-combinations-with-replacement/problem)

Is there a way to do this in R?

Prometheus
  • 673
  • 3
  • 25
  • 1
    I think you're asking for 'unique combinations' rather than 'with replacement', which to me is about sampling and means something different. Using that search term you'll see there are some related questions already, e.g. https://stackoverflow.com/questions/49563565/r-find-all-possible-unique-combinations – arvi1000 Apr 18 '19 at 03:54
  • @arvi1000 You are correct. My bad for confusing the two. Thanks for pointing me out in the right direction. – Prometheus Apr 18 '19 at 04:50

1 Answers1

1

Here's an alternative using expand.grid by creating a unique key for every combination and then removing duplicates

library(dplyr)

expand.grid(df1$Id1,df1$Id1) %>%
   mutate(key = paste(pmin(Var1, Var2), pmax(Var1, Var2), sep = "-")) %>%
   filter(!duplicated(key)) %>%
   select(-key) %>%
   mutate(row = row_number())


#  Var1 Var2 row
#1    1    1   1
#2    2    1   2
#3    3    1   3
#4    2    2   4
#5    3    2   5
#6    3    3   6
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213