Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. # Computing relative values between two rows of data, returned when grouping by "MatchId", one for each team in the match.
  2. # Script solution for Laura de Jong
  3.  
  4. ## function to compute relative scores between two rows, to use next in mutate
  5. fun = function(x){
  6. c(x[1] - x[2], x[2]-x[1])
  7. }
  8.  
  9. ## use function above on particular variables and rename
  10. rel_scores = TeamStats %>%
  11. group_by(MatchId) %>%
  12. mutate_at(vars(Appearances:GkShortAccuracy), list(~ fun)) %>% # run the function above over variables Appearances to GkShortAccuracy (the scoring variables)
  13. rename_at(vars(Appearances:GkShortAccuracy), ~ paste0(names(TeamStats %>% select(Appearances:GkShortAccuracy)), "_rel")) # rename calculated variables starting with "rel_"
  14.  
  15. ## join datasets, with and without relative scores, together
  16. TeamStats_join_rel = TeamStats %>%
  17. inner_join(rel_scores, by = c("Date", "MatchId", "TeamId", "OppositionId", "Venue", "LeagueOrTournament")) %>%
  18. select(noquote(order(colnames(.)))) %>% ## order variables by alphabetic order
  19. select(c("Date", "MatchId", "TeamId", "OppositionId", "Venue", "LeagueOrTournament"), everything()) %>% ## place independent variables at left, followed by everything else
  20. arrange(MatchId)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement