Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Computing relative values between two rows of data, returned when grouping by "MatchId", one for each team in the match.
- # Script solution for Laura de Jong
- ## function to compute relative scores between two rows, to use next in mutate
- fun = function(x){
- c(x[1] - x[2], x[2]-x[1])
- }
- ## use function above on particular variables and rename
- rel_scores = TeamStats %>%
- group_by(MatchId) %>%
- mutate_at(vars(Appearances:GkShortAccuracy), list(~ fun)) %>% # run the function above over variables Appearances to GkShortAccuracy (the scoring variables)
- rename_at(vars(Appearances:GkShortAccuracy), ~ paste0(names(TeamStats %>% select(Appearances:GkShortAccuracy)), "_rel")) # rename calculated variables starting with "rel_"
- ## join datasets, with and without relative scores, together
- TeamStats_join_rel = TeamStats %>%
- inner_join(rel_scores, by = c("Date", "MatchId", "TeamId", "OppositionId", "Venue", "LeagueOrTournament")) %>%
- select(noquote(order(colnames(.)))) %>% ## order variables by alphabetic order
- select(c("Date", "MatchId", "TeamId", "OppositionId", "Venue", "LeagueOrTournament"), everything()) %>% ## place independent variables at left, followed by everything else
- arrange(MatchId)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement