Batting Averages

Athletes’ statistics such as the batting average of a baseball player are regularly publicized and are a topic of discussion among sports enthusiasts. The batting average is computed by dividing the number of successful hits by the number of times the player has been at bat. A “time at bat” is every time the player leaves the home base after receiving pitches, but some exceptions apply. The batting averages of 446 baseball players were downloaded from Sean Lahman’s Baseball Archive at baseball1.com.
MATH221
sports
athletics
Author

MATH 221

Published

March 16, 2024

Data details

There are 446 rows and 6 columns. The data source1 is used to create our data that is stored in our pins table. You can access this pin from a connection to posit.byui.edu using hathawayj/batting_averages.

This data is available to all.

Variable description

  • Name: The name of the baseball player
  • Team: The team they belong to
  • League: The league they belong to
  • BattingAvg: A metric that measures batter success. Determined by dividing the player’s hits by their total at-bats.
  • AtBats: Number of times the player has been at bat, or attempted to hit the ball
  • Hits: Number of successful hits the player has made

Variable summary

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
BattingAvg 0 1 0.26 0.03 0.14 0.24 0.26 0.28 0.36 ▁▃▇▅▁
AtBats 0 1 341.96 163.53 101.00 197.25 334.00 487.50 682.00 ▇▆▅▅▃
Hits 0 1 91.94 49.94 16.00 46.00 83.00 131.00 225.00 ▇▇▆▅▁

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
Name 0 1 8 21 0 416 0
Team 0 1 12 29 0 30 0
League 0 1 15 15 0 2 0
Explore generating code using R
library(tidyverse)
library(pins)
library(connectapi)

batting_averages <- read_csv('https://github.com/byuistats/data/raw/master/BattingAverages/BattingAverages.csv')


# Publish the data to the server with Bro. Hathaway as the owner.
board <- board_connect()
pin_write(board, batting_averages, type = "parquet", access_type = "all")

pin_name <- "batting_averages"
meta <- pin_meta(board, paste0("hathawayj/", pin_name))
client <- connect()
my_app <- content_item(client, meta$local$content_id)
set_vanity_url(my_app, paste0("data/", pin_name))

Access data

This data is available to all.

Direct Download: batting_averages.parquet

R and Python Download:

URL Connections:

For public data, any user can connect and read the data using pins::board_connect_url() in R.

library(pins)
url_data <- "https://posit.byui.edu/data/batting_averages/"
board_url <- board_connect_url(c("dat" = url_data))
dat <- pin_read(board_url, "dat")

Use this custom function in Python to have the data in a Pandas DataFrame.

import pandas as pd
import requests
from io import BytesIO

def read_url_pin(name):
  url = "https://posit.byui.edu/data/" + name + "/" + name + ".parquet"
  response = requests.get(url)
  if response.status_code == 200:
    parquet_content = BytesIO(response.content)
    pandas_dataframe = pd.read_parquet(parquet_content)
    return pandas_dataframe
  else:
    print(f"Failed to retrieve data. Status code: {response.status_code}")
    return None

# Example usage:
pandas_df = read_url_pin("batting_averages")

Authenticated Connection:

Our connect server is https://posit.byui.edu which you assign to your CONNECT_SERVER environment variable. You must create an API key and store it in your environment under CONNECT_API_KEY.

Read more about environment variables and the pins package to understand how these environment variables are stored and accessed in R and Python with pins.

library(pins)
board <- board_connect(auth = "auto")
dat <- pin_read(board, "hathawayj/batting_averages")
import os
from pins import board_rsconnect
from dotenv import load_dotenv
load_dotenv()
API_KEY = os.getenv('CONNECT_API_KEY')
SERVER = os.getenv('CONNECT_SERVER')

board = board_rsconnect(server_url=SERVER, api_key=API_KEY)
dat = board.pin_read("hathawayj/batting_averages")