2016-08-21 9 views
0

ヘイズ、類似度indice

類似度indiceを計算して、行が 'simialr'でない場合は+1を取得したいと考えています。

dataR<- read.table(text=' 
    echant espece 
    ech1 esp1 
    ech2 esp2 
    ech3 esp2 
    ech4 esp3 
    ech5 esp3 
    ech6 esp4 
    ech7 esp4', header=TRUE) 

私はそのような行列を取得したいと思いますまあ、私は

library(proxy)  
trst<-read.table("Rtest_simil.csv",header=T,sep=",",dec=".") 
    is.numeric(trst[,2]) 
    as.numeric(trst[,2]) #the column "espece" becomes numeric 
    sim<-simil(trst,diag=TRUE) 

simil機能とプロキシパッケージを試してみました enter image description here

(DIAG上またはNAを、本当に重要されません)しかし、結果は私が望んだものではありません。 1)例えば2と3との間の類似性は0.5であり、対角線は0である。類似点もない場合は、0でもあります。 2)echのラベルが失われました。 3)...追加情報として、.csv形式で保存できません。

enter image description here

誰もがアドバイスを持っていますか? ありがとう!

答えて

1

これを行うには、よりコンパクトな方法は疑いありません:として列と行に名前を割り当てるには

same.mat <- outer(dataR$espece, dataR$espece, "==") * 2 - 1 

library(tidyr) 
same <- function(x) { ifelse(is.na(x), -1, 1) } 
spread(dataR, espece, espece) %>% 
    mutate_at(vars(-echant), funs(same)) 
## echant esp1 esp2 esp3 esp4 
## 1 ech1 1 -1 -1 -1 
## 2 ech2 -1 1 -1 -1 
## 3 ech3 -1 1 -1 -1 
## 4 ech4 -1 -1 1 -1 
## 5 ech5 -1 -1 1 -1 
## 6 ech6 -1 -1 -1 1 
## 7 ech7 -1 -1 -1 1 
+0

どうもありがとう、私はそれがまた作品、 – catindri

2

がポストに記述行列を用いて得ることができますポストに記載されていますrownamesとcolnamesを使用することができます。

rownames(same.mat) <- colnames(same.mat) <- dataR$echant 
> same.mat 
#  ech1 ech2 ech3 ech4 ech5 ech6 ech7 
#ech1 1 -1 -1 -1 -1 -1 -1 
#ech2 -1 1 1 -1 -1 -1 -1 
#ech3 -1 1 1 -1 -1 -1 -1 
#ech4 -1 -1 -1 1 1 -1 -1 
#ech5 -1 -1 -1 1 1 -1 -1 
#ech6 -1 -1 -1 -1 -1 1 1 
#ech7 -1 -1 -1 -1 -1 1 1 

別のアプローチが考えられます。

same.mat <- (as.matrix(dist(as.numeric(dataR$espece)))==0)*2 - 1 
rownames(same.mat) <- colnames(same.mat) <- dataR$echant 
+0

ありがとう... Rにprogaming改善する必要があります – catindri