Skip to content

Instantly share code, notes, and snippets.

@finiterank
Last active August 29, 2015 14:01
Show Gist options
  • Save finiterank/fdded039b5b4b5edab52 to your computer and use it in GitHub Desktop.
Save finiterank/fdded039b5b4b5edab52 to your computer and use it in GitHub Desktop.
Generador de una representación gráfica de la matriz de distancias entre candidatos
require(stats)
# Datos
candidatos <- c(1, 0, 0, 0, 0, 1, 1, 0, 0, 0.5, 0, 0.5, 0, 0, 0.5, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0.5, 0, 1, 1, 0, 0.5, 1, 0, 0.5, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0.5, 1, 1, 0, 0, 0.5, 0, 0, 1, 0, 0.5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.5, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1)
candidatos <- t(matrix(candidatos, nrow=5, ncol=18))
dimnames(candidatos) <- list(1:18, c("Peñalosa", "López", "Zuluaga", "Ramírez", "Santos"))
distancias <- dist(t(candidatos), method = "euclidean", diag = T, upper = T)
distancias <- data.matrix(distancias)
# Gráfica
image(1:5, 1:5, distancias, axes = FALSE, xlab="", ylab="")
axis(1, 1:5, dimnames(candidatos)[[2]], cex.axis = .8)
axis(2, 1:5, dimnames(candidatos)[[2]], cex.axis = .8, las=2)
for (i in 1:5)
{
for (j in 1:5)
{
txt <- sprintf("%0.1f", distancias[i,j])
text(i, j, txt, cex=1)
}
}
# Centroide:
centroide <- rowMeans(candidatos)
# Distancias al centroide
a <- data.matrix(dist(t(cbind(candidatos,centroide)), diag=T, upper=T))
a[6,1:5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment