0

对于那些熟悉层次分析过程的人来说,这应该更直观一些......

使用以下代码创建 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 循环来完成,但我不能完全掌握它,我的时间已经不多了。

4

1 回答 1

2

我想这就是你要找的:

l <- lower.tri(criteria)
criteria[l] <- 1/t(criteria)[l]

print(criteria)
##               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

仅供参考,这不起作用:

criteria[lower.tri(criteria)] <- 1/criteria[upper.tri(criteria)]

您希望下三角形的列用上三角形的行的倒数填充。但是,R 按列读取矩阵,因此criteria[upper.tri(criteria)]将返回以下内容:

 [1] 5.0000000 3.0000000 0.3333333 1.0000000 0.2000000 0.3333333 3.0000000 0.2000000 3.0000000 5.0000000

这可以通过取转置得到下三角形来解决,相当于取上三角形的行。

于 2016-10-11T23:23:30.593 回答