I am trying to use R to study basketball possession data. I have a table that looks like the table below, but with about 52 rows.
P1 | P2 | P3 | P4 | P5 | O.Points | O.Poss | D.Points | D.Poss |
---|---|---|---|---|---|---|---|---|
20 | 11 | 14 | 3 | 35 | 8 | 8 | 4 | 6 |
20 | 1 | 2 | 21 | 35 | 1 | 1 | 2 | 2 |
21 | 1 | 2 | 3 | 33 | 0 | 1 | 0 | 0 |
What I then did was create a new list that the sums of all the O.Points, O.Poss, D.Points, and D.Poss for every player by their jersey number regardless of if they appeared as P1, P2, P3, P4, or P5. The lists each looked as follows
Num | O.Points | O.Poss | D.Points | D.Poss |
---|---|---|---|---|
20 | 9 | 9 | 6 | 8 |
then I am turning those into numeric vectors, and then into a dataframe that looked like this
Num | O.Points | O.Poss | D.Points | D.Poss |
---|---|---|---|---|
20 | 9 | 9 | 6 | 8 |
1 | 1 | 2 | 2 | 2 |
2 | 1 | 2 | 2 | 2 |
35 | 9 | 9 | 6 | 8 |
Currently I am creating the lists by filtering for if any variable is equal to the player's number like this:
1 <- df %>% filter_at(vars(P1, P2, P3, P4, P5), any_vars(. %in% c('1')))
2 <- df %>% filter_at(vars(P1, P2, P3, P4, P5), any_vars(. %in% c('2')))
then turning them into vectors using the following code:
1 <- c(1, 1 %>% summarise_if(is.numeric, sum))
2 <- c(2, 2 %>% summarise_if(is.numeric, sum))
I am hoping to make a few new datasets that will track the same 4 data points(O.Points, O.Poss, D.Points, D.Poss) for every two man combination, three man combination, four man combination, and five man combination of players in my data.
Essentially any row that contains combination of two players has their final four columns (O.Points, O.Poss, D.Points, D.Poss) are summed together. This process should occur for every two player combination that were on the court together. Here is an example
Num1 | Num2 | O.Points | O.Poss | D.Points | D.Poss |
---|---|---|---|---|---|
1 | 2 | 1 | 2 | 2 | 2 |
20 | 1 | 1 | 1 | 2 | 2 |
20 | 21 | 0 | 0 | 0 | 0 |
I would hope to do the same thing for 3, 4, and 5 man lineups as well (I can provide an example if needed but I hope I've explained well enough)