Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distribute values to on arbitrary number of receivers

Tags:

r

I have data from some games, where, in every game, a variable number of players scored goals. I now want to distribute the number of goals in every single game, to the players that participated in that game. And I finally like to get the sum of goals every player has scored in all games combined.

Example:

Game 1: Players A + B + C; Goals: 3; so everyone gets a score of 1
Game 2: Players A + B + D + E; Goals: 8; everyone gets a score of 2
Game 3: Players B + C; Goals: 4; everyone gets a score of 2

Results: (this is what if want to create)

A: 3
B: 5
C: 3
D: 2
E: 2

This data is available in a CSV, where the variable players per game are put into a single column separated by a pipe (|):

Players;Goals
A|B|C;3
A|B|D|E;8
B|C;4
E;3

I can read this into a data.frame:

data <- read.csv("demo.csv", header=TRUE, sep=";")

and also separate the players info from the Players column in the data.frame:

lapply(data$Players, function(x) strsplit(as.character(x), "|", fixed=TRUE))

How do I distribute the scores from the Goals column to these players?

like image 224
MerlinDE Avatar asked Dec 21 '25 01:12

MerlinDE


1 Answers

Here's something using base functions and *apply:

#input data (from clipboard)
data <- read.table(header=TRUE,sep=";",file='clipboard')
players <- strsplit(as.character(data$Players),"|",fixed=TRUE)
#number of players in a game
data$n.player <- sapply(players,length)
#unique list of players
uni.players <- unique(unlist(players))

goals.per.player <- sapply(uni.players,function(x) {
  #identifies which games (rows of data) each player was in
  games.played <- which(sapply(players, function(y) x %in% y))
  #sums the games played
  sum((data$Goals/data$n.player)[games.played])
})
#A B C D E 
#3 5 3 2 5
like image 183
Blue Magister Avatar answered Dec 22 '25 19:12

Blue Magister



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!