Question

I have a list signatures, and I loop through all the combinations in that list. Then, I calculate the distance between the combinations:

for (signature_combination in combn(signatures,2, simplify=FALSE))
   score(signature_combination[[1]],signature_combination[[2]])
   name1 <- signature_combination[[1]]$name
   name2 <- signature_combination[[2]]$name
}

name1 and name2 are the names of the signatures to compare, which are the row and column names in the distance matrix.

Now I want to add this to a dataframe so that I get a distance matrix. I know the 3 ways of adding to a dataframe mentioned here: https://stat.ethz.ch/pipermail/r-help/2006-June/107734.html. However, this assumes that the values for the whole row are already known, but I only have 1 value per each time that I loop through, and they are not in order.

How can I add these values to a dataframe so that I get a distance matrix?

Was it helpful?

Solution

Why are you using a loop in the first place? I'd use expand.grid and by to do the job:

comb = expand.grid(names(signature), names(signature))   # I fixed this line!
scores = by(comb,list(comb$Var1,comb$Var2), FUN=function(x) score(signature[[x[[1]]]],signature[[x[[2]]]]))
class(scores)="matrix"
scores
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top