R中方法之间的胜负计数矩阵

R中方法之间的胜负计数矩阵,r,dataframe,R,Dataframe,假设我有一个数据框,其中每列都是一个方法,每行都是这种方法的度量(越低越好) 我想获得一个数据帧,其中包含所有方法(可能不止两个)之间的赢和输计数,如果一个方法的度量比另一个方法的度量小,则该方法获胜。像这样: +----------+-----------+-----------+-----------+-----------+ | | Method 1+ | Method 1- | Method 2+ | Method 2- | +----------+----------

假设我有一个数据框,其中每列都是一个方法,每行都是这种方法的度量(越低越好)

我想获得一个数据帧,其中包含所有方法(可能不止两个)之间的赢和输计数,如果一个方法的度量比另一个方法的度量小,则该方法获胜。像这样:

+----------+-----------+-----------+-----------+-----------+
|          | Method 1+ | Method 1- | Method 2+ | Method 2- |
+----------+-----------+-----------+-----------+-----------+
| Method 1 |         - |         - |         0 |         2 |
| Method 2 |         2 |         0 |         - |         - |
+----------+-----------+-----------+-----------+-----------+
其中,方法名称中的“+”表示该方法获胜,或表示该方法失败时为“-”

简单的方法是迭代数据帧的每一行,并在所有列对之间进行比较,但效率非常低


R中是否有更优雅的解决方案?

实际上,您不需要在该矩阵中使用太多的数据点来保存所有相同的信息;
方法1+
方法2
行(方法1击败方法2 x次数)将始终等于
方法2-
方法1
行(方法2输给方法1 x次数)。因此,我们可以这样得到这些信息:

# 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
#首先,我们制作一个函数,在两列中统计获胜数
#(这将有助于以后的feed应用)

count_wins你实际上不需要在这个矩阵中有那么多数据点来保存所有相同的信息;
方法1+
方法2
行(方法1击败方法2 x次数)将始终等于
方法2-
方法1
行(方法2输给方法1 x次数)。因此,我们可以这样得到这些信息:

# 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
#首先,我们制作一个函数,在两列中统计获胜数
#(这将有助于以后的feed应用)

在你的问题中加入a将增加你得到答案的机会。你还想概述一下你的赢/输逻辑是如何运作的,或者你是如何从你的输入中得到你想要的结果的。在你的问题中加入a将增加你得到答案的机会。你还想概述你的赢/输逻辑是如何工作的,或者你是如何从你的输入中得到你想要的结果的。这是有效的!谢谢@duckmayr(我不知道
combn
方法,它真的很好!)这很有效!谢谢@duckmayr(不知道
combn
方法,真的很好!)