实际上,您并不需要这个矩阵中的许多数据点来保存所有相同的信息; (方法1击败方法2×次数)将总是等于行的Method 2-
(方法2丢失到方法1×次数)。因此,我们可以得到如下信息:
# First we make a function to count the wins in two columns
# (this will be useful later to feed to apply)
count_wins <- function(columns, data) {
return(sum(data[ , columns[1]] < data[ , columns[2]]))
}
# Then we set the seed for some reproducible data
set.seed(123)
# Create some random example data
df <- data.frame(method1=sample(1:10, 5, replace=TRUE),
method2=sample(1:10, 5, replace=TRUE),
method3=sample(1:10, 5, replace=TRUE))
# method1 method2 method3
# 1 3 1 10
# 2 8 6 5
# 3 5 9 7
# 4 9 6 6
# 5 10 5 2
# We make an empty matrix to store results
result <- matrix(NA, nrow=ncol(df), ncol=ncol(df))
# Create a matrix of all column pairings
combos <- combn(x=ncol(df), m=2)
# And use apply, upper/lower.tri, and count_wins to fill the matrix
result[upper.tri(result)] <- apply(combos, 2, count_wins, df)
result[lower.tri(result)] <- apply(combos[2:1,], 2, count_wins, df)
# Then we just name the rows and columns
rownames(result) <- colnames(result) <- paste0('method', 1:3)
# method1 method2 method3
# method1 NA 1 2
# method2 4 NA 1
# method3 3 3 NA
这给了我们一个矩阵,其中每行告诉我们行方法击败列方法的次数。例如,这里跳动method1
一次method2
和method3
两次,而method2
次method1
四次method3
一次,等
我不知道这是不是你要找的“优雅”的解决方案,但它应该工作比循环更快,并为您提供所有相同信息的较小结果矩阵。
在你的问题中包括一个[最小可重现的例子](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)将增加你获得答案的机会。 – jsb
你也许想要概述你的赢/输逻辑是如何工作的,或者你如何从你的输入中得到你想要的结果。 –