对于那些熟悉层次分析过程的人来说,这应该更直观一些......
使用以下代码创建 data.frame criteria
:
c <- 5
cri_names <- c("Applicability", "Deployment", "Scalability", "Ease-of-Use", "TCO")
c1 <- data.frame(c = rep(0, c))
criteria <- data.frame(do.call("cbind", rep(c1, c)))
colnames(criteria) <- cri_names
rownames(criteria) <- cri_names
cvs <- data.frame(c1 = c(1, 5, 3, 1, 3),
c2 = c(1, 1, 1/3, 1/5, 1/5),
c3 = c(1, 1, 1, 1/3, 3),
c4 = c(1, 1, 1, 1, 5),
c5 = c(1, 1, 1, 1, 1))
for(v in c(1:ncol(cvs))) {
criteria[v, ] <- cvs[, v]
}
print(criteria)
# Applicability Deployment Scalability Ease-of-Use TCO
#Applicability 1 5 3.0000000 1.0000000 3.0
#Deployment 1 1 0.3333333 0.2000000 0.2
#Scalability 1 1 1.0000000 0.3333333 3.0
#Ease-of-Use 1 1 1.0000000 1.0000000 5.0
#TCO 1 1 1.0000000 1.0000000 1.0
我现在想要做的是用它对面的值的倒数替换1
左边的每一个。criteria[x, x]
例如:
criteria["Deployment", "Applicability"] <- 1/criteria["Applicability", "Deployment"]
最终结果应如下所示:
# Applicability Deployment Scalability Ease-of-Use TCO
#Applicability 1.0000000 5 3.0000000 1.0000000 3.0
#Deployment 0.2000000 1 0.3333333 0.2000000 0.2
#Scalability 0.3333333 3 1.0000000 0.3333333 3.0
#Ease-of-Use 1.0000000 5 3.0000000 1.0000000 5.0
#TCO 0.3333333 5 0.3333333 0.2000000 1.0
我非常有信心这可以通过嵌套的 for 循环来完成,但我不能完全掌握它,我的时间已经不多了。