R 为元素之间的关系构造对称矩阵
有一个矩阵:R 为元素之间的关系构造对称矩阵,r,R,有一个矩阵: 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" [
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"
然后我建立了两个矩阵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)
}
顺便说一下,我不喜欢环^^ 您可以尝试:
x<-strsplit(infor[,2],";")
y<-expand.grid(x,x)
matrix(mapply(function(.x,.y)
length(intersect(.x,.y)),y[[1]],y[[2]]),
nrow=nrow(infor),dimnames=list(infor[,1],infor[,1]))
# 1st 2nd 3rd 4th 5th 6th
#1st 3 1 2 1 1 0
#2nd 1 4 2 0 2 1
#3rd 2 2 3 0 1 0
#4th 1 0 0 3 1 1
#5th 1 2 1 1 3 1
#6th 0 1 0 1 1 2
xcheck函数?外部
function(aa, bb, Overlaps)
x<-strsplit(infor[,2],";")
y<-expand.grid(x,x)
matrix(mapply(function(.x,.y)
length(intersect(.x,.y)),y[[1]],y[[2]]),
nrow=nrow(infor),dimnames=list(infor[,1],infor[,1]))
# 1st 2nd 3rd 4th 5th 6th
#1st 3 1 2 1 1 0
#2nd 1 4 2 0 2 1
#3rd 2 2 3 0 1 0
#4th 1 0 0 3 1 1
#5th 1 2 1 1 3 1
#6th 0 1 0 1 1 2