Inspired by this Reddit thread, we are looking for players with “palindromic careers.” For example: Tom Glavine played 16 seasons for the Braves, then five seasons for the Mets, and then one final season with the Braves. So his career went Braves > Mets > Braves.
I am mainly interested in players with more than two different franchises though, i.e. A-B-C-B-A rather than just A-B-A.
The player/team/season data in the Lahman library is organized by teamID, but I want to use franchID instead. (For example, the Brooklyn Dodgers and Los Angeles Dodgers have distinct teamIDs, but the same franchID, to reflect the historical continuity of the franchise.) This can be achieved by merging the Lahman::Batting dataframe with Lahman::Teams.
head(player_data)
## yearID stint playerID teamID franchID
## 1 2004 1 aardsda01 SFN SFG
## 2 2006 1 aardsda01 CHN CHC
## 3 2007 1 aardsda01 CHA CHW
## 4 2008 1 aardsda01 BOS BOS
## 5 2009 1 aardsda01 SEA SEA
## 6 2010 1 aardsda01 SEA SEA
Example of what the functions do.
get_player() returns the list of all team-seasons for one player, arranged in ascending chronological order. (Ruth was on the Boston Red Sox 1914-19, the New York Yankees 1920-34 and the Boston Braves in 1935.)
list_pare() removes consecutive duplicates from the list, e.g. Ruth’s career is reduced to “BOS” “NYY” “ATL” (the “ATL” recognizes that the Boston Braves are part of the modern-day Atlanta Braves franchise.)
pal_test() determines whether a list is palindromic. Ruth’s career began and ended in Boston, but the Red Sox and Braves aren’t the same franchise. So that’s not a palindromic career.
# get the teams Babe Ruth played for
get_player("ruthba01")
## [1] "BOS" "BOS" "BOS" "BOS" "BOS" "BOS" "NYY" "NYY" "NYY" "NYY" "NYY" "NYY"
## [13] "NYY" "NYY" "NYY" "NYY" "NYY" "NYY" "NYY" "NYY" "NYY" "ATL"
# remove consecutive duplicates from the list
list_pare(get_player("ruthba01"))
## [1] "BOS" "NYY" "ATL"
# see if we have a palindromic career
pal_test(list_pare(get_player("ruthba01")))
## [1] FALSE
After testing the entire MLB player data set, we find three players in MLB history with the pattern A-B-C-B-A.
Note that Harry Dorish played for the St Louis Browns in 1950 and the Baltimore Orioles in 1955-56, but we treated those two teams as the same franchise.
output
## playerID pal teams nameFirst nameLast
## 1 dorisha01 TRUE c("BOS", "BAL", "CHW", "BAL", "BOS") Fritz Dorish
## 2 shawtr01 TRUE c("BOS", "MIL", "TOR", "MIL", "BOS") Travis Shaw
## 3 tinslle01 TRUE c("SEA", "BOS", "PHI", "BOS", "SEA") Lee Tinsley