data(Appearances)
# some test cases
# Henry Aaron spent the last two years of his career as DH in Milwaukee
subset(Appearances, playerID == 'aaronha01')
# Herb Washington, strictly a pinch runner for Oakland in 1974-5
subset(Appearances, playerID == 'washihe01')
subset(Appearances, playerID == 'thomeji01')
subset(Appearances, playerID == 'hairsje02')
# Appearances for the 1984 Cleveland Indians
subset(Appearances, teamID == "CLE" & yearID == 1984)
if (require(reshape2) & require(plyr)) {
# Appearances for Pete Rose during his career:
prose <- subset(Appearances, playerID == "rosepe01")
# What was Pete Rose's primary position each year
# of his career?
prose_melt <- melt(prose, id = c("yearID", "teamID"),
measure = 9:17)
# Split out the position from variable
prose_melt <- cbind(prose_melt, colsplit(prose_melt$variable,
"_", names = c("G", "pos")))
# Two grouping variables because of an in-season trade in 1984
primary_pos <- ddply(prose_melt, .(yearID, teamID), summarise,
top_pos = pos[which.max(value)],
games = max(value))
primary_pos
# Most pitcher appearances each year since 1950
ddply(subset(Appearances, yearID >= 1950), .(yearID), summarise,
maxPitcher = playerID[which.max(G_p)],
maxAppear = max(G_p))
# Individuals who have played all 162 games since 1961
all162 <- ddply(subset(Appearances, yearID > 1960), .(yearID), summarise,
allGamers = playerID[G_all == 162])
# Number of all-gamers by year
table(all162$yearID)
}
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(Lahman)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Lahman/Appearances.Rd_%03d_medium.png", width=480, height=480)
> ### Name: Appearances
> ### Title: Appearances table
> ### Aliases: Appearances
> ### Keywords: datasets
>
> ### ** Examples
>
> data(Appearances)
>
> # some test cases
> # Henry Aaron spent the last two years of his career as DH in Milwaukee
> subset(Appearances, playerID == 'aaronha01')
yearID teamID lgID playerID G_all GS G_batting G_defense G_p G_c G_1b
37138 1954 ML1 NL aaronha01 122 NA 122 122 0 0 0
37804 1955 ML1 NL aaronha01 153 NA 153 153 0 0 0
38427 1956 ML1 NL aaronha01 153 NA 153 153 0 0 0
39048 1957 ML1 NL aaronha01 151 NA 151 151 0 0 0
39685 1958 ML1 NL aaronha01 153 NA 153 153 0 0 0
40308 1959 ML1 NL aaronha01 154 NA 154 154 0 0 0
40948 1960 ML1 NL aaronha01 153 NA 153 153 0 0 0
41649 1961 ML1 NL aaronha01 155 NA 155 155 0 0 0
42384 1962 ML1 NL aaronha01 156 NA 156 156 0 0 1
43131 1963 ML1 NL aaronha01 161 NA 161 161 0 0 0
43883 1964 ML1 NL aaronha01 145 NA 145 145 0 0 0
44630 1965 ML1 NL aaronha01 150 NA 150 150 0 0 0
44931 1966 ATL NL aaronha01 158 NA 158 158 0 0 0
45705 1967 ATL NL aaronha01 155 NA 155 155 0 0 0
46491 1968 ATL NL aaronha01 160 NA 160 160 0 0 14
47206 1969 ATL NL aaronha01 147 NA 147 147 0 0 4
48138 1970 ATL NL aaronha01 150 NA 150 150 0 0 11
49057 1971 ATL NL aaronha01 139 NA 139 139 0 0 71
49939 1972 ATL NL aaronha01 129 NA 129 129 0 0 109
50827 1973 ATL NL aaronha01 120 NA 120 120 0 0 0
51719 1974 ATL NL aaronha01 112 89 112 89 0 0 0
53119 1975 ML4 AL aaronha01 137 129 137 3 0 0 0
54012 1976 ML4 AL aaronha01 85 70 85 1 0 0 0
G_2b G_3b G_ss G_lf G_cf G_rf G_of G_dh G_ph G_pr
37138 0 0 0 105 0 11 116 NA NA NA
37804 27 0 0 26 0 105 126 NA NA NA
38427 0 0 0 0 0 152 152 NA NA NA
39048 0 0 0 1 69 83 150 NA NA NA
39685 0 0 0 0 39 120 153 NA NA NA
40308 0 5 0 0 13 144 152 NA NA NA
40948 2 0 0 0 3 151 153 NA NA NA
41649 0 2 0 0 71 88 154 NA NA NA
42384 0 0 0 0 83 71 153 NA NA NA
43131 0 0 0 0 0 161 161 NA NA NA
43883 11 0 0 0 0 139 139 NA NA NA
44630 0 0 0 0 0 148 148 NA NA NA
44931 2 0 0 0 4 158 158 NA NA NA
45705 1 0 0 0 11 141 152 NA NA NA
46491 0 0 0 0 0 151 151 NA NA NA
47206 0 0 0 0 0 144 144 NA NA NA
48138 0 0 0 0 0 125 125 NA NA NA
49057 0 0 0 1 0 59 60 NA NA NA
49939 0 0 0 0 0 15 15 NA NA NA
50827 0 0 0 87 0 18 105 NA NA NA
51719 0 0 0 89 0 0 89 0 23 0
53119 0 0 0 3 0 0 3 126 8 0
54012 0 0 0 1 0 0 1 70 15 0
> # Herb Washington, strictly a pinch runner for Oakland in 1974-5
> subset(Appearances, playerID == 'washihe01')
yearID teamID lgID playerID G_all GS G_batting G_defense G_p G_c G_1b
52394 1974 OAK AL washihe01 92 0 92 0 0 0 0
53307 1975 OAK AL washihe01 13 0 13 0 0 0 0
G_2b G_3b G_ss G_lf G_cf G_rf G_of G_dh G_ph G_pr
52394 0 0 0 0 0 0 0 0 0 92
53307 0 0 0 0 0 0 0 0 0 13
> subset(Appearances, playerID == 'thomeji01')
yearID teamID lgID playerID G_all GS G_batting G_defense G_p G_c G_1b
68820 1991 CLE AL thomeji01 27 27 27 27 0 0 0
69892 1992 CLE AL thomeji01 40 37 40 40 0 0 0
70977 1993 CLE AL thomeji01 47 45 47 47 0 0 0
72109 1994 CLE AL thomeji01 98 90 98 94 0 0 0
73211 1995 CLE AL thomeji01 137 129 137 134 0 0 0
74473 1996 CLE AL thomeji01 151 145 151 150 0 0 0
75701 1997 CLE AL thomeji01 147 140 147 145 0 0 145
76995 1998 CLE AL thomeji01 123 120 123 117 0 0 117
78301 1999 CLE AL thomeji01 146 143 146 111 0 0 111
79629 2000 CLE AL thomeji01 158 154 158 107 0 0 107
80991 2001 CLE AL thomeji01 156 149 156 148 0 0 148
82311 2002 CLE AL thomeji01 147 146 147 128 0 0 128
84226 2003 PHI NL thomeji01 159 157 159 156 0 0 156
85570 2004 PHI NL thomeji01 143 140 143 134 0 0 134
86849 2005 PHI NL thomeji01 59 57 59 52 0 0 52
87490 2006 CHA AL thomeji01 143 139 143 3 0 0 3
88871 2007 CHA AL thomeji01 130 124 130 1 0 0 1
90247 2008 CHA AL thomeji01 149 139 149 0 0 0 0
91644 2009 CHA AL thomeji01 107 98 107 0 0 0 0
92099 2009 LAN NL thomeji01 17 0 17 0 0 0 0
93576 2010 MIN AL thomeji01 108 78 108 0 0 0 0
94521 2011 CLE AL thomeji01 93 78 93 1 0 0 0
95587 2012 BAL AL thomeji01 58 39 58 4 0 0 4
G_2b G_3b G_ss G_lf G_cf G_rf G_of G_dh G_ph G_pr
68820 0 27 0 0 0 0 0 0 0 0
69892 0 40 0 0 0 0 0 0 1 0
70977 0 47 0 0 0 0 0 0 2 0
72109 0 94 0 0 0 0 0 0 7 0
73211 0 134 0 0 0 0 0 1 8 0
74473 0 150 0 0 0 0 0 1 5 1
75701 0 0 0 0 0 0 0 0 7 0
76995 0 0 0 0 0 0 0 6 3 0
78301 0 0 0 0 0 0 0 34 3 0
79629 0 0 0 0 0 0 0 48 4 0
80991 0 0 0 0 0 0 0 5 7 0
82311 0 0 0 0 0 0 0 18 1 0
84226 0 0 0 0 0 0 0 2 2 0
85570 0 0 0 0 0 0 0 6 3 0
86849 0 0 0 0 0 0 0 5 2 0
87490 0 0 0 0 0 0 0 136 4 0
88871 0 0 0 0 0 0 0 123 6 0
90247 0 0 0 0 0 0 0 139 10 0
91644 0 0 0 0 0 0 0 98 9 0
92099 0 0 0 0 0 0 0 0 17 0
93576 0 0 0 0 0 0 0 78 30 0
94521 0 1 0 0 0 0 0 78 15 0
95587 0 0 0 0 0 0 0 36 19 0
> subset(Appearances, playerID == 'hairsje02')
yearID teamID lgID playerID G_all GS G_batting G_defense G_p G_c G_1b
76747 1998 BAL AL hairsje02 6 2 6 4 0 0 0
78062 1999 BAL AL hairsje02 50 49 50 50 0 0 0
79360 2000 BAL AL hairsje02 49 49 49 49 0 0 0
80740 2001 BAL AL hairsje02 159 154 159 156 0 0 0
82071 2002 BAL AL hairsje02 122 118 122 119 0 0 0
83387 2003 BAL AL hairsje02 58 56 58 48 0 0 0
84743 2004 BAL AL hairsje02 86 80 86 62 0 0 0
86179 2005 CHN NL hairsje02 114 91 114 104 0 0 0
87505 2006 CHN NL hairsje02 38 21 38 32 0 0 1
88515 2006 TEX AL hairsje02 63 24 63 56 0 0 0
89903 2007 TEX AL hairsje02 73 41 73 71 0 0 0
90316 2008 CIN NL hairsje02 80 62 80 73 0 0 0
91710 2009 CIN NL hairsje02 86 77 86 81 0 0 0
92210 2009 NYA AL hairsje02 45 21 45 41 0 0 0
93829 2010 SDN NL hairsje02 119 105 119 114 0 0 0
94856 2011 MIL NL hairsje02 120 92 120 112 0 0 0
96063 2012 LAN NL hairsje02 78 63 78 74 0 0 1
97362 2013 LAN NL hairsje02 96 43 96 63 0 0 13
G_2b G_3b G_ss G_lf G_cf G_rf G_of G_dh G_ph G_pr
76747 4 0 0 0 0 0 0 0 0 3
78062 50 0 0 0 0 0 0 0 0 1
79360 49 0 0 0 0 0 0 0 0 0
80740 156 0 0 0 0 0 0 0 0 3
82071 119 0 0 0 0 0 0 0 2 2
83387 48 0 0 0 0 0 0 8 1 1
84743 12 1 0 10 15 27 52 20 2 3
86179 44 0 1 20 48 1 62 0 15 1
87505 24 0 0 3 0 5 8 0 7 2
88515 1 1 3 35 12 7 52 0 4 13
89903 16 10 2 21 22 6 49 0 6 5
90316 7 1 36 24 17 12 42 0 10 0
91710 9 33 31 9 3 5 17 0 7 0
92210 3 16 11 10 2 9 19 0 4 8
93829 47 3 62 4 0 8 12 0 7 1
94856 30 49 2 23 20 0 42 0 13 4
96063 30 32 2 18 0 0 18 1 10 0
97362 1 28 0 23 0 4 27 0 39 1
>
> # Appearances for the 1984 Cleveland Indians
> subset(Appearances, teamID == "CLE" & yearID == 1984)
yearID teamID lgID playerID G_all GS G_batting G_defense G_p G_c G_1b
61490 1984 CLE AL apontlu01 25 0 0 25 25 0 0
61491 1984 CLE AL bandoch01 75 60 75 64 0 63 1
61492 1984 CLE AL barklje01 3 0 0 3 3 0 0
61493 1984 CLE AL behenri01 3 0 0 3 3 0 0
61494 1984 CLE AL bernato01 140 122 140 136 0 0 0
61495 1984 CLE AL blylebe01 33 0 0 33 33 0 0
61496 1984 CLE AL butlebr01 159 149 159 156 0 0 0
61497 1984 CLE AL camacer01 69 0 0 69 69 0 0
61498 1984 CLE AL cartejo01 66 55 66 60 0 0 7
61499 1984 CLE AL castica01 87 56 87 70 0 0 0
61500 1984 CLE AL comerst01 22 0 0 22 22 0 0
61501 1984 CLE AL easteja01 26 0 0 26 26 0 0
61502 1984 CLE AL farrst01 31 0 0 31 31 0 0
61503 1984 CLE AL fischmi01 85 45 85 83 0 0 0
61504 1984 CLE AL francju01 160 158 160 159 0 0 0
61505 1984 CLE AL frazige01 22 0 0 22 22 0 0
61506 1984 CLE AL hallme01 83 65 83 69 0 0 0
61507 1984 CLE AL hargrmi01 133 85 133 124 0 0 124
61508 1984 CLE AL hassero01 48 39 48 45 0 44 1
61509 1984 CLE AL heatone01 38 0 0 38 38 0 0
61510 1984 CLE AL jacobbr01 126 125 126 126 0 0 0
61511 1984 CLE AL jeffcmi01 63 0 0 63 63 0 0
61512 1984 CLE AL moronje01 7 5 7 6 0 0 0
61513 1984 CLE AL nixonot01 49 29 49 46 0 0 0
61514 1984 CLE AL noboaju01 23 1 23 19 0 0 0
61515 1984 CLE AL perkibr01 58 7 58 2 0 0 2
61516 1984 CLE AL quirkja01 1 0 1 1 0 1 0
61517 1984 CLE AL rhombke01 13 2 13 9 0 0 1
61518 1984 CLE AL romanjo02 3 0 0 3 3 0 0
61519 1984 CLE AL romerra01 1 0 0 1 1 0 0
61520 1984 CLE AL schuldo01 19 0 0 19 19 0 0
61521 1984 CLE AL smithro01 22 0 0 22 22 0 0
61522 1984 CLE AL spillda01 14 0 0 14 14 0 0
61523 1984 CLE AL sutclri01 15 0 0 15 15 0 0
61524 1984 CLE AL tablepa01 144 131 144 139 0 0 67
61525 1984 CLE AL thornan01 155 155 155 11 0 0 11
61526 1984 CLE AL ujdurje01 4 0 0 4 4 0 0
61527 1984 CLE AL vukovge01 134 110 134 130 0 0 0
61528 1984 CLE AL waddeto01 58 0 0 58 58 0 0
61529 1984 CLE AL willaje01 87 68 87 76 0 76 0
G_2b G_3b G_ss G_lf G_cf G_rf G_of G_dh G_ph G_pr
61490 0 0 0 0 0 0 0 0 0 NA
61491 0 1 0 0 0 0 0 1 13 0
61492 0 0 0 0 0 0 0 0 0 NA
61493 0 0 0 0 0 0 0 0 0 NA
61494 136 0 0 0 0 0 0 0 3 4
61495 0 0 0 0 0 0 0 0 0 NA
61496 0 0 0 0 156 1 156 0 3 2
61497 0 0 0 0 0 0 0 0 0 NA
61498 0 0 0 48 14 0 59 0 8 1
61499 0 0 0 0 0 70 70 0 26 1
61500 0 0 0 0 0 0 0 0 0 NA
61501 0 0 0 0 0 0 0 0 0 NA
61502 0 0 0 0 0 0 0 0 0 NA
61503 55 17 15 0 0 0 0 0 2 7
61504 0 0 159 0 0 0 0 0 1 1
61505 0 0 0 0 0 0 0 0 0 NA
61506 0 0 0 64 1 6 69 9 13 0
61507 0 0 0 0 0 0 0 0 20 0
61508 0 0 0 0 0 0 0 1 3 0
61509 0 0 0 0 0 0 0 0 0 NA
61510 0 126 1 0 0 0 0 0 0 0
61511 0 0 0 0 0 0 0 0 0 NA
61512 0 6 0 0 0 0 0 0 1 1
61513 0 0 0 43 4 0 46 0 0 10
61514 19 0 0 0 0 0 0 0 0 6
61515 0 0 0 0 0 0 0 6 50 1
61516 0 0 0 0 0 0 0 0 0 0
61517 1 0 0 7 0 0 7 0 3 4
61518 0 0 0 0 0 0 0 0 0 NA
61519 0 0 0 0 0 0 0 0 0 NA
61520 0 0 0 0 0 0 0 0 0 NA
61521 0 0 0 0 0 0 0 0 0 NA
61522 0 0 0 0 0 0 0 0 0 NA
61523 0 0 0 0 0 0 0 0 0 NA
61524 1 36 0 43 0 0 43 1 8 4
61525 0 0 0 0 0 0 0 144 0 0
61526 0 0 0 0 0 0 0 0 0 NA
61527 0 0 0 16 0 124 130 0 15 0
61528 0 0 0 0 0 0 0 0 0 NA
61529 0 0 0 0 0 0 0 1 12 0
>
>
> if (require(reshape2) & require(plyr)) {
+ # Appearances for Pete Rose during his career:
+ prose <- subset(Appearances, playerID == "rosepe01")
+
+
+ # What was Pete Rose's primary position each year
+ # of his career?
+
+ prose_melt <- melt(prose, id = c("yearID", "teamID"),
+ measure = 9:17)
+ # Split out the position from variable
+ prose_melt <- cbind(prose_melt, colsplit(prose_melt$variable,
+ "_", names = c("G", "pos")))
+
+ # Two grouping variables because of an in-season trade in 1984
+ primary_pos <- ddply(prose_melt, .(yearID, teamID), summarise,
+ top_pos = pos[which.max(value)],
+ games = max(value))
+ primary_pos
+
+ # Most pitcher appearances each year since 1950
+ ddply(subset(Appearances, yearID >= 1950), .(yearID), summarise,
+ maxPitcher = playerID[which.max(G_p)],
+ maxAppear = max(G_p))
+
+ # Individuals who have played all 162 games since 1961
+ all162 <- ddply(subset(Appearances, yearID > 1960), .(yearID), summarise,
+ allGamers = playerID[G_all == 162])
+ # Number of all-gamers by year
+ table(all162$yearID)
+ }
Loading required package: reshape2
Loading required package: plyr
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1973 1974 1975 1976 1977
2 6 3 9 5 5 1 3 5 2 2 6 4 3 2 4
1978 1979 1980 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1996
4 8 7 8 4 5 4 5 2 2 5 2 3 4 2 3
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
6 4 5 5 7 6 5 3 10 6 7 4 1 2 1 4
2013 2014
207 4
>
>
>
>
>
>
> dev.off()
null device
1
>