Home > OS >  How to repeat redundant combinations using the `combn` function in R?
How to repeat redundant combinations using the `combn` function in R?

Time:07-22

I have a dataframe like this:

df1 <- data.frame(
  letter = letters[1:4]
)

I am running the combn function on this dataframe:

df2 <- df1 %>% 
  dplyr::summarise(
    combo = combn(df1$letter, 2, stringr::str_c, collapse = '_')
  )

Which gives this:

combo
a_b
a_c
a_d
b_c
b_d
c_d

However, the way that I am subsequently wrangling the data requires me to list all combinations despite repetitions. The combn function seems to drop the repeated groups. That said, I'd like my result to look like this:

combo
a_b
a_c
a_d
b_a
b_c
b_d
c_a
c_b
c_d
d_a
d_b
d_c

Is there a workaround that I can use to achieve this result? I am open to using something other than combn if there's a better way to make this work.

CodePudding user response:

Here is a tidy solution:

df1 <- data.frame(
  letter = letters[1:4],
  letter1 = letters[1:4]
)

purrr::cross_df(df1) %>%
  dplyr::filter(letter != letter1) %>%
  tidyr::unite(combo, c(letter1, letter), sep = "_")

CodePudding user response:

Here are three solutions with package arrangements.

1. Without a pipe

a <- arrangements::permutations(letters[1:4], 2)
data.frame(combo = apply(a, 1, paste, collapse = "_"))
#>    combo
#> 1    a_b
#> 2    a_c
#> 3    a_d
#> 4    b_a
#> 5    b_c
#> 6    b_d
#> 7    c_a
#> 8    c_b
#> 9    c_d
#> 10   d_a
#> 11   d_b
#> 12   d_c

Created on 2022-07-21 by the reprex package (v2.0.1)

2. With a pipe

Except for package permutations all other functions are base functions. Output omitted.

letters[1:4] |>
  arrangements::permutations(2) |>
  apply(1, paste, collapse = "_") |>
  as.data.frame() |>
  `names<-`('combo')

Created on 2022-07-21 by the reprex package (v2.0.1)

3. With a pipe

A pipe plus tidyverse functions. Output omitted.

letters[1:4] |>
  arrangements::permutations(2) |>
  as.data.frame() |>
  dplyr::mutate(
    combo = stringr::str_c(V1, V2, sep = '_')
  ) |>
  dplyr::select(-V1, -V2)

Created on 2022-07-21 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related