I would like to count co-occurences of a variable across groups. I'm sure there's an easy {dplyr}/{tidyr} solution with count or pivot_ but I'm stuck. Thank you for your help! Below is a reprex of my input and potential output (I don't care about the format of the output, could also be a contingency table). Note that order of the variable does not matter, i.e., "green" then "blue" is the same as "blue" then "green".
library(dplyr)
df_in <- tribble(
~id, ~color,
1, "green",
1, "blue",
2, "blue",
2, "green",
3, "blue",
3, "red"
)
df_out <- tribble(
~colors, ~n,
"blue-green", 2,
"blue-red", 1
)
Or as a contingency table: | | blue |green|red| | ----- | - | - | - | | blue | - | 2 | 1 | | green | 2 | - | 0 | | red | 1 | 0 | - |
