1

I have a dataframe with 2 columns - id and term - and I am trying to make all possible combinations of term according to the id. Here's an example: df <- dataframe (id = c(1,1,1,1,2,2,3), term = c(aaa, aab, aac, aad, aba, abb, aaa))

This is the result I would like to obtain:

  1. 1 aaa aab
  2. 1 aaa aac
  3. 1 aaa aad
  4. 1 aab aac
  5. 1 aab aad
  6. 1 aac aad
  7. 2 aba abb

I tried crating two equal datasets a and b, and combining them with this code

a <- df
b <- df
for (p in a) {
  for (q in b) {
    if (b$id == a$id)
     rez <- rbin(crossing(a$term, b$term))
  }
}

but the result is just all possible combinations of term.

Any suggestions how to solve this? Thanks!

1 Answers1

0

I assume there's some function I've never heard of but here's how I would do it.

Join them up, sort the 2 columns alpabetically then remove duplicates and where x=y

df <- data.frame (id = c(1,1,1,1,2,2,3),term = c('aaa', 'aab', 'aac', 'aad', 'aba', 'abb', 'aaa'))

full_join(df, df, by = 'id') %>%
  transmute(
    id,
    x1 = if_else(term.x < term.y, term.x, term.y),
    x2 = if_else(term.x < term.y, term.y, term.x),
  ) %>%
  filter(x1 != x2) %>%
  distinct()

using dplyr

Quixotic22
  • 2,894
  • 1
  • 6
  • 14