This function parses the height string (in ft and in) and converts to float
# convert height string from ft and in to ft
parse_height <- function(height_str) {
# split str into ft and in components
height_parts <- strsplit(height_str, "' ")[[1]]
feet <- as.numeric(height_parts[1])
inches <- as.numeric(gsub('"', '', height_parts[2]))
# convert height to ft units
height_in_feet <- feet + inches / 12
return(height_in_feet)
}
This chunks the ID list to ensure no single API call has more than 500 IDs
chunk_list <- function(lst, chunk_size) {
split(lst, ceiling(seq_along(lst) / chunk_size))
}
The API call below takes player_id integer vector as input and builds a data frame with id, height and weight
# fetch player heights given their IDs
get_player_heights <- function(player_ids) {
all_data <- list()
# split player IDs into chunks
chunks <- chunk_list(player_ids, 500)
for (chunk in chunks) {
# convert the chunk of player IDs to a comma-separated string
player_id_str <- paste(chunk, collapse = ",")
url <- paste0('https://statsapi.mlb.com/api/v1/people?personIds=', player_id_str, '&fields=people,id,height,weight')
# get the response from the API
response <- GET(url)
# check if the request was successful
if (http_status(response)$category != "Success") {
stop("HTTP request failed")
}
# parse the JSON response
json_data <- content(response, as = "parsed", simplifyVector = TRUE)$people
# convert to a data frame and apply height parsing
data <- as.data.frame(json_data)
data$height <- sapply(data$height, parse_height)
all_data <- append(all_data, list(data))
# sleep to avoid overloading server
Sys.sleep(0.5)
}
# combine chunks into a single data frame
result_df <- bind_rows(all_data)
return(result_df)
}
We can now fetch the heights of a couple of players
# example player IDs
player_ids <- c(694973, 573186)
# fetch the data
player_df <- get_player_heights(player_ids)
head(player_df)
## id height weight
## 1 694973 6.500000 235
## 2 573186 5.583333 180