'How to create combinations of certain values in dataframe in R? [duplicate]
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 aaa aab
- 1 aaa aac
- 1 aaa aad
- 1 aab aac
- 1 aab aad
- 1 aac aad
- 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!
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Quixotic22 |
