R/url2playergamelogs.R

#' @title URL 2 Player Game Logs
#' @param url url for player game logs
#' @description gets dataframe of game logs given url
#' @keywords NBA basketball-reference html
#' @importFrom magrittr %>%
#' @export
#' @examples
#' url2playergamelogs(url,player,year)

url2playergamelogs <- function(url, player, year) {

  cont <- getContent(url)

  if (length(cont) > 0) {

    df <- cont[[1]]

    # rename columns that were unnamed
    names(df)[c(6,8)] <- c("nonsense", "game_outcome")

    if (ncol(df) > 29) {
      df <- df[,1:30]
    }

    #plus/minus only recorded year 2001 and after
    if (year>=2001) {
      df <- df %>%
        dplyr::select(-nonsense,-Rk) %>%
        dplyr::rename(game = G,
                      date = Date,
                      age = Age,
                      team = Tm,
                      opp = Opp,
                      started_game = GS,
                      minutes_played = MP,
                      field_goals = FG,
                      field_goals_attempted = FGA,
                      field_goal_pct = `FG%`,
                      three_pt_fg = `3P`,
                      three_pts_attm = `3PA`,
                      three_pt_pct = `3P%`,
                      ft_made = FT,
                      ft_attm = FTA,
                      ft_pct = `FT%`,
                      off_reb = ORB,
                      def_reb = DRB,
                      tot_reb = TRB,
                      assists = AST,
                      steals = STL,
                      blocks = BLK,
                      turnovers = TOV,
                      fouls = PF,
                      points = PTS,
                      game_score = GmSc,
                      plus_minus = `+/-`)
    } else {
      df <- df %>%
        dplyr::select(-nonsense,-Rk) %>%
        dplyr::rename(game = G,
                      date = Date,
                      age = Age,
                      team = Tm,
                      opp = Opp,
                      started_game = GS,
                      minutes_played = MP,
                      field_goals = FG,
                      field_goals_attempted = FGA,
                      field_goal_pct = `FG%`,
                      three_pt_fg = `3P`,
                      three_pts_attm = `3PA`,
                      three_pt_pct = `3P%`,
                      ft_made = FT,
                      ft_attm = FTA,
                      ft_pct = `FT%`,
                      off_reb = ORB,
                      def_reb = DRB,
                      tot_reb = TRB,
                      assists = AST,
                      steals = STL,
                      blocks = BLK,
                      turnovers = TOV,
                      fouls = PF,
                      points = PTS,
                      game_score = GmSc)
    }

    return (df)

  } else {

    cat(player, 'did not play in', year)

    return (NA)

  }
}
emilykuehler/basketballstatsR documentation built on May 31, 2019, 10:01 a.m.