Skip to content
Prev 306752 / 398506 Next

Transform pairwise observations into a table

Another base-R-only solution uses a 2-column matrix of subscripts
to fill a matrix.  E.g.,
+     mat <- matrix(NA_real_, nrow=max(data[[1]]), ncol=max(data[[2]]))
+     mat[cbind(data[[1]], data[[2]])] <- data[[3]]
+     mat
+ }
[,1]  [,2]  [,3] [,4]
[1,] 1.000 0.250 0.125  0.5
[2,] 0.250 1.000 0.125  0.5
[3,] 0.125 0.125 1.000  0.5
[4,] 0.500 0.500 0.500  1.0

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com