我正在尝试计算一长串序列之间的成对差异的数量,并将其放回矩阵形式。这是我想做的一个玩具示例。
library(MiscPsycho)
b <- c("-BC", "ACB", "---") # Toy example of sequences
workb <- expand.grid(b,b)
new <- c(1:9)
# Need to get rid of this for loop somehow
for (i in 1:9) {
new[i] <- stringMatch(workb[i,1], workb[i,2], normalize="NO")
}
workb <- cbind(workb, new)
newmat <- reShape(workb$new, id=workb$Var1, colvar=workb$Var2)
a <- c("Subject1", "Subject2", "Subject3") #Relating it back to the subject ID
colnames(newmat) <- a
rownames(newmat) <- a
newmat
我对使用 apply 函数不是很熟悉,但我想用它来替换 for 循环,考虑到我有大量序列,这可能会变慢。(stringMatch 函数来自 MiscPsycho)。请让我知道如何提高效率!
非常感谢!