I want to transform a data.frame to an adjacency matrix. In my data, I got articles and authors (several columns, one for each coauthor), where each row is an article. I want authors of the same article to be tied.
The data structure is now like this:
data <- data.frame(Author1 = c("Alan", "Rebecca", "Micheal", "Dany", "Euron", "Alan"),
Author2 = c("Rebecca", NA, "Alan", "Euron", NA, "Dany"),
Author3 = c("Dany", NA, "Euron", "Micheal", NA, NA),
Author4 = c("Euron", NA, "Rebecca", NA, NA, NA),
Title = c("Eric's boat", "Top 100 boats", "Boats in the World", "Death and boats", "Boats and Dragons", "Boats"))
Which gives this output:
+---------+---------+---------+---------+--------------------+
| Author 1| Author2 | Author3 | Author4 | Title |
+---------+---------+---------+---------+--------------------+
| Alan | Rebecca | Dany | Euron | Eric's boat |
| Rebecca | NA | NA | NA | Top 100 boats |
| Micheal | Alan | Euron | Rebecca | Boats in the world |
| Dany | Euron | Micheal | NA | Death and boats |
| Euron | NA | NA | NA | Boats and Dragons |
| Alan | Dany | NA | NA | Boats |
+---------+---------+---------+---------+--------------------+
I want it to look like this:
+---------+------+---------+---------+------+-------+
| | Alan | Rebecca | Micheal | Dany | Euron |
+---------+------+---------+---------+------+-------+
| Alan | 0 | 1 | 0 | 1 | 1 |
| Rebecca | 1 | 0 | 1 | 0 | 0 |
| Micheal | 1 | 1 | 1 | 1 | 1 |
| Dany | 1 | 0 | 1 | 0 | 1 |
| Euron | 1 | 0 | 1 | 1 | 0 |
+---------+------+---------+---------+------+-------+