R中的Rowsums由多列中的唯一值

Rowsums in R by unique values in multiple columns

假设我有一个 table 棋手对(只是一个想象的例子)。 table 显示谁下了白棋、黑棋以及玩家之间的游戏次数。

| White| Black| Games|              
|:---- |:------:| -----:|               
| Anand| Caruana| 13 |         
| Carlsen| Naka| 12 |             
| Caruana| Giri| 14 |          
| Giri| Anand| 10 |           
| Grischuk| Carlsen| 7|    

我想要的是每个玩家的游戏总数(黑+白),即他与任何其他大师对战的所有游戏。

| Player | Games_total|           
|:---- |:------:|            
| Anand| 33|      
| Caruana| 27|            
| Carlsen| 21|         
| Naka| 12|           
| Giri| 34| 
| Grischuk| 9| 
   

您可以获得长格式的数据,然后 sum 每个 PlayerGames

library(dplyr)
library(tidyr)

df %>%
  pivot_longer(cols = c(White, Black), values_to = 'Player') %>%
  group_by(Player) %>%
  summarise(Games_total = sum(Games))

# Player   Games_total
#  <chr>          <int>
#1 Anand             23
#2 Carlsen           19
#3 Caruana           27
#4 Giri              24
#5 Grischuk           7
#6 Naka              12

数据

df <- structure(list(White = c("Anand", "Carlsen", "Caruana", "Giri", 
"Grischuk"), Black = c("Caruana", "Naka", "Giri", "Anand", "Carlsen"
), Games = c(13L, 12L, 14L, 10L, 7L)), row.names = c(NA, -5L), class = "data.frame")

使用 aggregate

尝试此解决方案
setNames( aggregate( Games ~ values, 
  cbind( stack(df1, c("White","Black")), Games=df1$Games), sum ), 
  c("Players","Games_total") )

   Players Games_total
1    Anand          23
2  Carlsen          19
3  Caruana          27
4     Giri          24
5 Grischuk           7
6     Naka          12

数据

df1 <- structure(list(White = c("Anand", "Carlsen", "Caruana", "Giri",
"Grischuk"), Black = c("Caruana", "Naka", "Giri", "Anand", "Carlsen"
), Games = c(13L, 12L, 14L, 10L, 7L)), class = "data.frame", row.names = c(NA,
-5L))