[英]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)
Player
);A
- (1) 中獲得A
分數原始數字,而不是它的兩倍;這給了你這個結果:
# 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.