mam matrycę dystans:Skutecznie korzystające parami odległości
> mat
hydrogen helium lithium beryllium boron
hydrogen 0.000000 2.065564 3.940308 2.647510 2.671674
helium 2.065564 0.000000 2.365661 1.697749 1.319400
lithium 3.940308 2.365661 0.000000 3.188148 2.411567
beryllium 2.647510 1.697749 3.188148 0.000000 2.499369
boron 2.671674 1.319400 2.411567 2.499369 0.000000
oraz ramkę danych:
> results
El1 El2 Score
Helium Hydrogen 92
Boron Helium 61
Boron Lithium 88
Chcę obliczyć wszystkie parami odległości między słowami w results$El1
i results$El2
aby uzyskać następnie:
> results
El1 El2 Score Dist
Helium Hydrogen 92 2.065564
Boron Helium 61 1.319400
Boron Lithium 88 2.411567
Zrobiłem to z pętlą for, ale wydaje się naprawdę niezgrabny. Czy istnieje bardziej elegancki sposób wyszukiwania i wyodrębniania odległości z mniejszą liczbą linii kodu?
Tu jest mój bieżący kod:
names = row.names(mat)
num.results <- dim(results)[1]
El1 = match(results$El1, names)
El2 = match(results$El2, names)
el.dist <- matrix(0, num.results, 1)
for (i1 in c(1:num.results)) {
el.dist[i1, 1] <- mat[El1[i1], El2[i1]]
}
results$Dist = el.dist[,1]
Usunąłem poprzedni komentarz po znalezieniu problem: nieuczciwe litery! – Dex