6

I'm not sure what this problem is even called. Let's say I'm counting distinct combinations of 2 columns, but I want distinct across the order of the two columns. Here's what I mean:

df = data.frame(fruit1 = c("apple", "orange", "orange", "banana", "kiwi"),
                fruit2 = c("orange", "apple", "banana", "orange", "apple"),
                stringsAsFactors = FALSE)

# What I want: total number of fruit combinations, regardless of 
# which fruit comes first and which second.
# Eg 2 apple-orange, 2 banana-orange, 1 kiwi-apple

# What I know *doesn't* work:

table(df$fruit1, df$fruit2) 

# What *does* work:
library(dplyr)
df %>% group_by(fruit1, fruit2) %>% 
  transmute(fruitA = sort(c(fruit1, fruit2))[1],
            fruitB = sort(c(fruit1, fruit2))[2]) %>%
  group_by(fruitA, fruitB) %>%
  summarise(combinations = n())

I've got a way to make this work, as you can see, but is there a name for this general problem? It's sort of a combinatorics problem but counting, not generating combinations. And what if I had three or four columns of similar type? The above method is poorly generalizable. Tidyverse approaches most welcome!

Joy
  • 769
  • 6
  • 24

2 Answers2

7

By using apply and sort order your dataframe then we just using group_by count

data.frame(t(apply(df,1,sort)))%>%group_by_all(.)%>%count()
# A tibble: 3 x 3
# Groups:   X1, X2 [3]
      X1     X2     n
  <fctr> <fctr> <int>
1  apple   kiwi     1
2  apple orange     2
3 banana orange     2
BENY
  • 317,841
  • 20
  • 164
  • 234
0

Here is an option using pmap with count

library(tidyverse)
library(rlang)
pmap_df(df, ~ sort(c(...)) %>%
                 as.list %>%
                 as_tibble %>%
                 set_names(names(df))) %>% 
    count(!!! rlang::syms(names(.)))
# A tibble: 3 x 3
#  fruit1 fruit2     n
#   <chr>  <chr>  <int>
#1 apple  kiwi       1
#2 apple  orange     2
#3 banana orange     2
akrun
  • 874,273
  • 37
  • 540
  • 662