-
Notifications
You must be signed in to change notification settings - Fork 1
/
mlbam.R
92 lines (81 loc) · 2.15 KB
/
mlbam.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#' Get MLB active rosters
#'
#' @param team `[numeric]` MLB [`statsapi`](https://statsapi.mlb.com/) team ID
#'
#' @return An object with class `c("tbl_df", "tbl", "data.frame")`. Specifically,
#' a data frame with `40` rows and `11` columns:
#' \describe{
#' \item{`JerseyNumber`}{`[character]` desc}
#' \item{`TeamID`}{`[integer]` desc}
#' \item{`PersonID`}{`[integer]` desc}
#' \item{`FullName`}{`[character]` desc}
#' \item{`PersonLink`}{`[character]` desc}
#' \item{`PositionCode`}{`[character]` desc}
#' \item{`PositionName`}{`[character]` desc}
#' \item{`PositionType`}{`[character]` desc}
#' \item{`PositionAbbreviation`}{`[character]` desc}
#' \item{`StatusCode`}{`[character]` desc}
#' \item{`StatusDescription`}{`[character]` desc}
#' }
#' @export
mlb_roster = function(team) {
# create MLB statsapi URL
# TODO: make lookup table or function
url = paste("https://statsapi.mlb.com/api/v1/teams/", team, "/roster/40Man", sep = "")
# acquire JSON, extract roster, flatten, add nicer names
# TODO: consider column types and NA or "" values
data = jsonlite::fromJSON(url)$roster
data = jsonlite::flatten(data)
if (ncol(data) == 12) {
# TODO: instead of removing, add to all rosters, even if no notes exist?
# TODO: what could/should notes be used for?
data$note = NULL
}
names(data) = c(
"JerseyNumber",
"TeamID",
"PersonID",
"FullName",
"PersonLink",
"PositionCode",
"PositionName",
"PositionType",
"PositionAbbreviation",
"StatusCode",
"StatusDescription"
)
# set class for table object
class(data) = table_class()
# return roster
return(data)
}
# 108 LAA Angels
# 109 ARI D-backs
# 110 BAL Orioles
# 111 BOS Red Sox
# 112 CHC Cubs
# 113 CIN Reds
# 114 CLE Indians
# 115 COL Rockies
# 116 DET Tigers
# 117 HOU Astros
# 118 KC Royals
# 119 LAD Dodgers
# 120 WSH Nationals
# 121 NYM Mets
# 133 OAK Athletics
# 134 PIT Pirates
# 135 SD Padres
# 136 SEA Mariners
# 137 SF Giants
# 138 STL Cardinals
# 139 TB Rays
# 140 TEX Rangers
# 141 TOR Blue Jays
# 142 MIN Twins
# 143 PHI Phillies
# 144 ATL Braves
# 145 CWS White Sox
# 146 MIA Marlins
# 147 NYY Yankees
# 158 MIL Brewers