Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rowsums in R by unique values in multiple columns

Suppose I have a table with chess players pairs (just an imaginary example). The table shows who played white, black and the number of games between the players.

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

What I want is the total number of games (Black+White) per player, i.e. all the games he played against any other grandmaster.

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You may get the data in long format and then sum the Games for each Player.

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

data

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading