I want to create a bipartite graph (in r) of Conditions and Treaters based on actual events. I can do this easily if I can get my data converted to the right format as in the table below:
| Physio | GP | Chemist | Psych | Dentist | |
|---|---|---|---|---|---|
| BackPain | 1 | 1 | 1 | 0 | 0 |
| Depression | 0 | 1 | 1 | 1 | 0 |
| Flu | 0 | 1 | 1 | 0 | 0 |
| Anxiety | 0 | 1 | 0 | 1 | 0 |
| Toothache | 0 | 0 | 0 | 0 | 1 |
For further clarification, the leftmost column rows are "Conditions" and the columns are "Treaters" (obviously ficticious), with the intersection = 1, if the Treater was vistied for the specific Condition.
My data is in long format below. It consists of a patient id, the term that contains the Condition/Treater word and a type which denotes if the term is a Condition or Treater type.
df <-
data.frame(
id = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5),
term = c("BackPain", "Physio", "GP", "Chemist", "Depression", "GP", "Chemist", "Psych", "Flu", "GP", "Chemist", "Anxiety", "GP", "Psych", "Toothache", "Dentist"),
type = c("Condition", "Treater", "Treater", "Treater", "Condition", "Treater", "Treater", "Treater", "Condition", "Treater", "Treater", "Condition", "Treater", "Treater", "Condition", "Treater")
)
I suspect I need a clever pivot_wider type solution OR bypass the above structure altogether to go direct from my source data to the igraph Bipartite format. I searched everywhere and I cannot find similar questions/answers where the data is in a long format.
Any ideas to: 1) convert the long format to the wide format or 2) how to go from the long format directly to an igraph bipartite graph?
Help will be greatly appreciated! Thanks

