繁体   English   中英

在 R 中查找行对的组合总和

[英]Finding combined sums of pairs of rows in R

我有一个带有测试结果的 dataframe(行是玩家;列 Q1...Q6 是不同的问题)。 现在我想找出哪对球员的总得分最高:

# Generating sample data.
n = 6

set.seed(1986)

results_df = data.frame(Player = c("A", "B", "C", "D", "E", "F"), 
                     Q1 = sample(0:1, size = n, replace = TRUE), 
                     Q2 = sample(0:1, size = n, replace = TRUE),
                     Q3 = sample(0:1, size = n, replace = TRUE),
                     Q4 = sample(0:1, size = n, replace = TRUE),
                     Q5 = sample(0:1, size = n, replace = TRUE),
                     Q6 = sample(0:1, size = n , replace = TRUE))


head(results_df)

  Player Q1 Q2 Q3 Q4 Q5 Q6
1      A  1  0  1  0  0  0
2      B  1  1  0  0  0  0
3      C  0  1  0  1  0  1
4      D  0  1  1  0  1  1
5      E  1  1  1  1  1  1
6      F  1  0  0  1  0  1

1 和 0 代表每个玩家的问题是正确 (1) 还是错误 (0)。 现在我想将每对球员结合起来,看看他们作为一对球员会做得如何。

有谁知道我如何将上面的 dataframe 转换为下面这样的东西?

(这里我只是手动总结了每个配对组合:A 有 3 个正确,加上 B 有 3 个正确但 A 错误的问题,组合为 6,依此类推......)

  Player  A  B  C  D  E  F
1      A  2  3  5  5  6  4
2      B  3  2  4  5  6  4
3      C  5  4  3  5  6  4
4      D  5  5  5  4  6  6
5      E  6  6  6  6  6  6
6      F  4  4  4  6  6  3

在基础 R 你可以这样做:

a <- data.frame(t(as.matrix(results_df[-1])))
b <- combn(a, 2, function(x)sum(x[1] | x[2]))
attributes(b) <- list(Size = ncol(a), Labels = results_df$Player)
d <- as.matrix(structure(b, class = 'dist'))
diag(d) <- colSums(a)
d
  A B C D E F
A 2 3 5 5 6 4
B 3 2 4 5 6 4
C 5 4 3 5 6 4
D 5 5 5 4 6 6
E 6 6 6 6 6 6
F 4 4 4 6 6 3

您可以使用它来获取总和

n <- 6

# get the combinations
ee <- expand.grid(1:n, 1:n)
res <- setNames(data.frame(matrix(
  rowSums(results_df[,-1][ee[,1],] | results_df[,-1][ee[,2],]), n)), 
  results_df[,1])

rownames(res) <- results_df[,1]

res
  A B C D E F
A 2 3 5 5 6 4
B 3 2 4 5 6 4
C 5 4 3 5 6 4
D 5 5 5 4 6 6
E 6 6 6 6 6 6
F 4 4 4 6 6 3

另一种选择,基于dplyr::coalesce

library(dplyr)
df <- na_if(results_df, 0)
mat <- lapply(seq(nrow(df)), function(x) sapply(seq(nrow(df)), function(y) dplyr::coalesce(df[x,-1], df[y,-1]))) |>
  lapply(function(x) colSums(matrix(as.numeric(x),nrow=6), na.rm=T))
mat <- do.call(rbind, mat)
colnames(mat) <- df$Player
rownames(mat) <- df$Player

mat
#   A B C D E F
# A 2 3 5 5 6 4
# B 3 2 4 5 6 4
# C 5 4 3 5 6 4
# D 5 5 5 4 6 6
# E 6 6 6 6 6 6
# F 4 4 4 6 6 3

使用此输入数据:

#   Player Q1 Q2 Q3 Q4 Q5 Q6
#1      A  0  1  1  0  1  1
#2      B  0  0  1  0  1  1
#3      C  1  1  0  0  0  0
#4      D  0  1  0  1  1  1
#5      E  1  1  0  1  0  1
#6      F  0  0  0  1  0  1

有简单的base解决方案:

scr <- rowSums(dat[, -1])                           # 1)
res <- data.frame(outer(scr, scr, '+') - diag(scr)) # 2)
dimnames(res) <- dat[, c(1, 1)]                     # 3)
  1. 查找每行的分数总和(即Player );
  2. 求 (1) 中获得的每对分数之间的总和。 从对角线上减去原始分数,例如对A - (1) 中获得A分数原始数字,而不是它的两倍;
  3. 将行名和列名设置为玩家姓名;

这给了你这个结果:

#   A B C D E F
# A 4 7 6 8 8 6
# B 7 3 5 7 7 5
# C 6 5 2 6 6 4
# D 8 7 6 4 8 6
# E 8 7 6 8 4 6
# F 6 5 4 6 6 2

数据:

dat <- structure(
  list(
    Player = c("A", "B", "C", "D", "E", "F"),
    Q1     = c(0, 0, 1, 0, 1, 0),
    Q2     = c(1, 0, 1, 1, 1, 0),
    Q3     = c(1, 1, 0, 0, 0, 0),
    Q4     = c(0, 0, 0, 1, 1, 1),
    Q5     = c(1, 1, 0, 1, 0, 0),
    Q6     = c(1, 1, 0, 1, 1, 1)
  ),
  row.names = c(NA,-6L),
  class = "data.frame"
)

一个基本的 R 选件,带outer

> lst <- asplit(`row.names<-`(as.matrix(results_df[-1]), results_df$Player), 1)

> outer(lst, lst, FUN = Vectorize(function(x, y) sum(x + y > 0)))
  A B C D E F
A 2 3 5 5 6 4
B 3 2 4 5 6 4
C 5 4 3 5 6 4
D 5 5 5 4 6 6
E 6 6 6 6 6 6
F 4 4 4 6 6 3

所以这里是计算所有玩家综合得分的代码。 我不知道,为什么你需要它们以矩阵形式但使用它你应该能够创建矩阵。 解决方案是使用tidyr::pivot_longer() ,然后使用 dplyr。

# Generating sample data.
set.seed(1986)

n <- 6
results_df <- data.frame(
  Player = c("A", "B", "C", "D", "E", "F"),
  Q1 = sample(0:1, size = n, replace = TRUE),
  Q2 = sample(0:1, size = n, replace = TRUE),
  Q3 = sample(0:1, size = n, replace = TRUE),
  Q4 = sample(0:1, size = n, replace = TRUE),
  Q5 = sample(0:1, size = n, replace = TRUE),
  Q6 = sample(0:1, size = n, replace = TRUE)
)


results_df
#>   Player Q1 Q2 Q3 Q4 Q5 Q6
#> 1      A  1  0  1  0  0  0
#> 2      B  1  1  0  0  0  0
#> 3      C  0  1  0  1  0  1
#> 4      D  0  1  1  0  1  1
#> 5      E  1  1  1  1  1  1
#> 6      F  1  0  0  1  0  1

results_df |>
  tidyr::pivot_longer(cols = tidyselect::starts_with("Q"), names_to = "question", values_to = "score") |>
  dplyr::group_by(Player) |>
  dplyr::summarise(total = sum(score))
#> # A tibble: 6 x 2
#>   Player total
#>   <chr>  <int>
#> 1 A          2
#> 2 B          2
#> 3 C          3
#> 4 D          4
#> 5 E          6
#> 6 F          3

代表 package (v2.0.1) 于 2022 年 2 月 4 日创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM