Closed jalapic closed 7 years ago
use this code:
homeaway <- function (df) {
hconf <- vconf <- leg <- hgoalaet <- vgoalaet <- hpen <- vpen <- ga <- Date <- Season <- team <- opp <- gf <- division <- tier <- venue <- home <- visitor <- hgoal <- vgoal <- NULL
if ("division" %in% colnames(df)) {
rbind(df %>% dplyr::select(Date, Season, team = home,
opp = visitor, gf = hgoal, ga = vgoal, division,
tier) %>% dplyr::mutate(venue = "home"), df %>% dplyr::select(Date,
Season, team = visitor, opp = home, gf = vgoal, ga = hgoal,
division, tier) %>% dplyr::mutate(venue = "away")) %>%
dplyr::arrange(team, Date)
}
else if("tier" %in% colnames(df)){
rbind(df %>% dplyr::select(Date, Season, team = home,
opp = visitor, gf = hgoal, ga = vgoal,
tier) %>% dplyr::mutate(venue = "home"), df %>% dplyr::select(Date,
Season, team = visitor, opp = home, gf = vgoal, ga = hgoal,
tier) %>% dplyr::mutate(venue = "away")) %>%
dplyr::arrange(team, Date)
}
else {
rbind(df %>% dplyr::select(Date, Season, team = home,
opp = visitor, gf = hgoal, ga = vgoal, teamconf = hconf,
oppconf = vconf, round, leg, gfaet = hgoalaet, gaaet = vgoalaet,
penfaet = hpen, penaaet = vpen) %>% dplyr::mutate(venue = "home"),
df %>% dplyr::select(Date, Season, team = visitor,
opp = home, gf = vgoal, ga = hgoal, teamconf = vconf,
oppconf = hconf, round, leg, gfaet = vgoalaet,
gaaet = hgoalaet, penfaet = vpen, penaaet = hpen) %>%
dplyr::mutate(venue = "away")) %>% dplyr::arrange(team,
Date)
}
}
homeaway()
does not reliably work with non-standard dataframes such as thespain
dataset. Needs to be updated.