我有一些游戏的数据,在每场比赛中,有不同数量的球员进球。我现在想要将每个游戏中的目标数量分配给参与该游戏的玩家。而且我最终希望得到所有球员在所有比赛中得分的目标总和。
示例:
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中的玩家列分开:
lapply(data$Players, function(x) strsplit(as.character(x), "|", fixed=TRUE))
如何将目标列中的分数分发给这些玩家?
答案 0 :(得分: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