I have a dataframe that looks like this:
Cluster.y
Cluster.x 1 2 3 4 5 7
1 0 1 0 12 0 2
2 4 1 0 6 1 5
3 0 0 0 2 0 0
4 6 9 0 23 10 9
5 1 0 0 15 5 3
7 5 4 1 18 4 6
It seems easy, but I couldn't find the solution to make it symmetric. My goal is to for example have all occurrences of Cluster 1 and Cluster 7 (= 7 occurrences), appear in both cells they are meeting in. I want to be able to read the dataframe as rows or columns with the same result. Can anyone point me in the right direction?
EDIT: the dput looks like this:
structure(c(0L, 4L, 0L, 6L, 1L, 5L, 1L, 1L, 0L, 9L, 0L, 4L, 0L,
0L, 0L, 0L, 0L, 1L, 12L, 6L, 2L, 23L, 15L, 18L, 0L, 1L, 0L, 10L,
5L, 4L, 2L, 5L, 0L, 9L, 3L, 6L), dim = c(6L, 6L), dimnames = list(
Cluster.x = c("1", "2", "3", "4", "5", "7"), Cluster.y = c("1",
"2", "3", "4", "5", "7")), class = c("xtabs", "table"), call = xtabs(formula = ~Cluster.x
Cluster.y, data = Example_cluster_7_20XX))
EDIT: the desired output looks like this (only showing [1:3,1:3]):
Cluster.y
Cluster.x 1 2 3
1 0 5 0
2 5 1 0
3 0 0 0
Notice that clusters 2 and 1 have 5 occurrences together. Before, they had 4 and 1. When I View(df) it looks like this:
Cluster.x Cluster.y Freq
1 1 0
2 2 1
1 2 1
2 1 4
etc..
CodePudding user response:
Perhaps this helps i.e. take the sum of the transpose and the matrix
`diag<-`(m1 t(m1), diag(m1))
-output
Cluster.y
Cluster.x 1 2 3 4 5 7
1 0 5 0 18 1 7
2 5 1 0 15 1 9
3 0 0 0 2 0 1
4 18 15 2 23 25 27
5 1 1 0 25 5 7
7 7 9 1 27 7 6