1
之間關係的對稱矩陣有一個矩陣:構建元素
infor <- cbind(c("1st","2nd","3rd","4th","5th","6th"), c("a;b;c","c;d;e;f","a;c;d","b;g;h","b;d;e","e;h"))
infor
[,1] [,2]
[1,] "1st" "a;b;c"
[2,] "2nd" "c;d;e;f"
[3,] "3rd" "a;c;d"
[4,] "4th" "b;g;h"
[5,] "5th" "b;d;e"
[6,] "6th" "e;h"
我想在infor[, 1]
來算,每兩個元素之間的重疊和構建這樣一個對稱矩陣:
> result
1st 2nd 3rd 4th 5th 6th
1st 3 1 2 1 1 0
2nd 1 4 2 1 2 1
3rd 2 2 3 1 1 0
4th 1 1 1 3 1 1
5th 1 2 1 1 3 1
6th 0 1 0 1 1 2
然後我bulid兩個矩陣aa和bb:
> aa <- matrix(rep(infor[, 2], dim(infor)[1]), nrow=dim(infor)[1])
> aa
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "a;b;c" "a;b;c" "a;b;c" "a;b;c" "a;b;c" "a;b;c"
[2,] "c;d;e;f" "c;d;e;f" "c;d;e;f" "c;d;e;f" "c;d;e;f" "c;d;e;f"
[3,] "a;c;d" "a;c;d" "a;c;d" "a;c;d" "a;c;d" "a;c;d"
[4,] "b;g;h" "b;g;h" "b;g;h" "b;g;h" "b;g;h" "b;g;h"
[5,] "b;d;e" "b;d;e" "b;d;e" "b;d;e" "b;d;e" "b;d;e"
[6,] "e;h" "e;h" "e;h" "e;h" "e;h" "e;h"
> bb <- t(aa)
> bb
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "a;b;c" "c;d;e;f" "a;c;d" "b;g;h" "b;d;e" "e;h"
[2,] "a;b;c" "c;d;e;f" "a;c;d" "b;g;h" "b;d;e" "e;h"
[3,] "a;b;c" "c;d;e;f" "a;c;d" "b;g;h" "b;d;e" "e;h"
[4,] "a;b;c" "c;d;e;f" "a;c;d" "b;g;h" "b;d;e" "e;h"
[5,] "a;b;c" "c;d;e;f" "a;c;d" "b;g;h" "b;d;e" "e;h"
[6,] "a;b;c" "c;d;e;f" "a;c;d" "b;g;h" "b;d;e" "e;h"
> Overlaps <- function(a, b){
spliteA <- strsplit(a, ";")
spliteB <- strsplit(b, ";")
score <- length(intersect(spliteA, spliteB))
return(score)
}
我不知道是否e是一個函數,我可以用來實現我的目標,就像
function(aa, bb, Overlaps)
順便說一下,我不喜歡循環。 ^^
檢查功能'outer' – agenis