Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make two random teams form dataset and get overall team score in R

Lets say I have a soccer team of 10 players (players) from which I should make two subteams of 5 players each and then compute the overall score for each team.

players <- read.table(text=
"paul    3
ringo   3
george  5
john    5
mick    1
ron     2
charlie 3
ozzy    5
keith   3
brian   3", as.is=TRUE)

I've already extracted a random set of 5 players:

t1 <- sample(players$V1, size = 5)

But one it comes to create the second team (excluding the players in the first one) and calculating the overall score for both teams I'm completed blocked.

like image 743
binarymonk Avatar asked Jan 09 '23 11:01

binarymonk


1 Answers

You could try sampling the indices of players to construct the first team instead of sampling the names.

idx1 <- sample(1:nrow(players), 5)

You can actually use these indices to grab all the information about each team:

team1 <- players[idx1,]
team2 <- players[-idx1,]

The score for each team can be computed with sum(team1$V2) and sum(team2$V2).

like image 153
josliber Avatar answered Jan 31 '23 08:01

josliber