I am trying to make a matrix in R, based on a dataframe with 3 columns: 1st column is names of people in a group, 2nd column is also names of people in a group, 3rd column is a value that indicates how well the people match:
Person1 Person2 Match
1 Amber Tiffany 5
2 Amber James 1
3 Amber Kenneth 7
4 Amber Gordon 9
5 Tiffany James 4
6 Tiffany Kenneth 6
7 Tiffany Gordon 6
8 James Kenneth 3
9 James Gordon 7
10 Kenneth Gordon 2
etc
(in fact it's many more names and values)
I want the names of the people on both axes of the matrix, like this:
Amber | Tiffany | James | Kenneth | Gordon | |
---|---|---|---|---|---|
Amber | 0 | 5 | 1 | 7 | 9 |
Tiffany | 5 | 0 | 4 | 6 | 6 |
James | 1 | 4 | 0 | 3 | 7 |
Kenneth | 7 | 6 | 3 | 0 | 2 |
Gordon | 9 | 6 | 7 | 2 | 0 |
I can't find an easy way to do this. I've read and tried several things, such as:
- Reshape three column data frame to matrix ("long" to "wide" format)
- R: Create 3 column dataframe from sparse matrix
- How to reshape data from long to wide format
- Reshape three column data frame to matrix ("long" to "wide" format)
But I didn't succeed yet.