3

我有一些比赛的数据,在每场比赛中,有不同数量的球员进球。我现在想将每场比赛的进球数分配给参加那场比赛的球员。最后我想得到每个球员在所有比赛中得分的总和。

示例

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

结果:(这就是如果要创建的内容)

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

数据以 CSV 格式提供,其中每场比赛的可变球员被放入由竖线 (|) 分隔的单个列中:

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

我可以将其读入data.frame:

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

并将玩家信息与 data.frame 中的 Players 列分开:

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

如何将进球栏中的分数分配给这些球员?

4

1 回答 1

2

这是使用base函数和的东西*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
于 2013-01-24T16:13:39.713 回答