# Baseball R Question

Hi, I may have a complicated question. In this project, I am dealing with Baseball Analytics. I want to find out if there is an apparent impact when a player (Rajai Davis) gets on base. I want to find out the batting average (H/AB) of the person who bats right after Rajai Davis. I believe I have all the data necessary to find out this answer, but I do not think I am skilled enough and would appreciate some help.

Rajai Davis batterid="davir003"
Rajai Davis teamid="CLE"

VB1 means visitor batter 1, VB2 means visitor batter 2
HB 1 means home batter 1, HB2 means home batter 2
H=Hit
AB=At bat
Batting Average = H/AB

So if Rajai batted first and it was a home game and he got a hit, I would like to see what happened to the batter who batted right after him (VB2). I know this may be confusing and not described the best. I am guessing my first steps is to filter by visiting_team="CLE" OR home_team="CLE".

``````
data.frame(
H = c(1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0),
AB = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0),
gameid = as.factor(c("ANA201604040", "ANA201604040", "ANA201604040",
"ANA201604040", "ANA201604040", "ANA201604040",
"ANA201604040", "ANA201604040", "ANA201604040",
"ANA201604040", "ANA201604040", "ANA201604040",
"ANA201604040", "ANA201604040", "ANA201604040",
"ANA201604040", "ANA201604040", "ANA201604040",
"ANA201604040", "ANA201604040")),
batterid = as.factor(c("fowld001", "heywj001", "zobrb001", "rizza001",
"schwk001", "solej001", "montm001", "pujoa001",
"calhk001", "cronc002", "simma001", "russa002",
"fowld001", "heywj001", "zobrb001", "perec003")),
visiting_team = as.factor(c("CHN", "CHN", "CHN", "CHN", "CHN", "CHN", "CHN",
"CHN", "CHN", "CHN", "CHN", "CHN", "CHN", "CHN",
"CHN", "CHN", "CHN", "CHN", "CHN", "CHN")),
home_team = as.factor(c("ANA", "ANA", "ANA", "ANA", "ANA", "ANA", "ANA",
"ANA", "ANA", "ANA", "ANA", "ANA", "ANA", "ANA",
"ANA", "ANA", "ANA", "ANA", "ANA", "ANA")),
VB1 = as.factor(c("fowld001", "fowld001", "fowld001", "fowld001",
"fowld001", "fowld001", "fowld001", "fowld001",
"fowld001", "fowld001", "fowld001", "fowld001",
"fowld001", "fowld001", "fowld001", "fowld001",
"fowld001", "fowld001", "fowld001", "fowld001")),
VB2 = as.factor(c("heywj001", "heywj001", "heywj001", "heywj001",
"heywj001", "heywj001", "heywj001", "heywj001",
"heywj001", "heywj001", "heywj001", "heywj001",
"heywj001", "heywj001", "heywj001", "heywj001",
"heywj001", "heywj001", "heywj001", "heywj001")),
VB3 = as.factor(c("zobrb001", "zobrb001", "zobrb001", "zobrb001",
"zobrb001", "zobrb001", "zobrb001", "zobrb001",
"zobrb001", "zobrb001", "zobrb001", "zobrb001",
"zobrb001", "zobrb001", "zobrb001", "zobrb001",
"zobrb001", "zobrb001", "zobrb001", "zobrb001")),
VB4 = as.factor(c("rizza001", "rizza001", "rizza001", "rizza001",
"rizza001", "rizza001", "rizza001", "rizza001",
"rizza001", "rizza001", "rizza001", "rizza001",
"rizza001", "rizza001", "rizza001", "rizza001",
"rizza001", "rizza001", "rizza001", "rizza001")),
VB5 = as.factor(c("bryak001", "bryak001", "bryak001", "bryak001",
"bryak001", "bryak001", "bryak001", "bryak001",
"bryak001", "bryak001", "bryak001", "bryak001",
"bryak001", "bryak001", "bryak001", "bryak001",
"bryak001", "bryak001", "bryak001", "bryak001")),
VB6 = as.factor(c("schwk001", "schwk001", "schwk001", "schwk001",
"schwk001", "schwk001", "schwk001", "schwk001",
"schwk001", "schwk001", "schwk001", "schwk001",
"schwk001", "schwk001", "schwk001", "schwk001",
"schwk001", "schwk001", "schwk001", "schwk001")),
VB7 = as.factor(c("solej001", "solej001", "solej001", "solej001",
"solej001", "solej001", "solej001", "solej001",
"solej001", "solej001", "solej001", "solej001",
"solej001", "solej001", "solej001", "solej001",
"solej001", "solej001", "solej001", "solej001")),
VB8 = as.factor(c("montm001", "montm001", "montm001", "montm001",
"montm001", "montm001", "montm001", "montm001",
"montm001", "montm001", "montm001", "montm001",
"montm001", "montm001", "montm001", "montm001",
"montm001", "montm001", "montm001", "montm001")),
VB9 = as.factor(c("russa002", "russa002", "russa002", "russa002",
"russa002", "russa002", "russa002", "russa002",
"russa002", "russa002", "russa002", "russa002",
"russa002", "russa002", "russa002", "russa002",
"russa002", "russa002", "russa002", "russa002")),
HB1 = as.factor(c("escoy001", "escoy001", "escoy001", "escoy001",
"escoy001", "escoy001", "escoy001", "escoy001",
"escoy001", "escoy001", "escoy001", "escoy001",
"escoy001", "escoy001", "escoy001", "escoy001",
"escoy001", "escoy001", "escoy001", "escoy001")),
HB3 = as.factor(c("troum001", "troum001", "troum001", "troum001",
"troum001", "troum001", "troum001", "troum001",
"troum001", "troum001", "troum001", "troum001",
"troum001", "troum001", "troum001", "troum001",
"troum001", "troum001", "troum001", "troum001")),
HB4 = as.factor(c("pujoa001", "pujoa001", "pujoa001", "pujoa001",
"pujoa001", "pujoa001", "pujoa001", "pujoa001",
"pujoa001", "pujoa001", "pujoa001", "pujoa001",
"pujoa001", "pujoa001", "pujoa001", "pujoa001",
"pujoa001", "pujoa001", "pujoa001", "pujoa001")),
HB5 = as.factor(c("calhk001", "calhk001", "calhk001", "calhk001",
"calhk001", "calhk001", "calhk001", "calhk001",
"calhk001", "calhk001", "calhk001", "calhk001",
"calhk001", "calhk001", "calhk001", "calhk001",
"calhk001", "calhk001", "calhk001", "calhk001")),
HB6 = as.factor(c("cronc002", "cronc002", "cronc002", "cronc002",
"cronc002", "cronc002", "cronc002", "cronc002",
"cronc002", "cronc002", "cronc002", "cronc002",
"cronc002", "cronc002", "cronc002", "cronc002",
"cronc002", "cronc002", "cronc002", "cronc002")),
HB7 = as.factor(c("simma001", "simma001", "simma001", "simma001",
"simma001", "simma001", "simma001", "simma001",
"simma001", "simma001", "simma001", "simma001",
"simma001", "simma001", "simma001", "simma001",
"simma001", "simma001", "simma001", "simma001")),
HB8 = as.factor(c("perec003", "perec003", "perec003", "perec003",
"perec003", "perec003", "perec003", "perec003",
"perec003", "perec003", "perec003", "perec003",
"perec003", "perec003", "perec003", "perec003",
"perec003", "perec003", "perec003", "perec003")),
HB9 = as.factor(c("giavj001", "giavj001", "giavj001", "giavj001",
"giavj001", "giavj001", "giavj001", "giavj001",
"giavj001", "giavj001", "giavj001", "giavj001",
"giavj001", "giavj001", "giavj001", "giavj001",
"giavj001", "giavj001", "giavj001", "giavj001"))
)
``````

In the simplest terms, my question is...

"If Rajai Davis gets a hit, what is the batting average of the player who bats right after him?"

I realize the data I have posted may not be helpful because it is not filtered to just be CLE games.

Last comment, it will not be the same player who bats after him every game. So there could be around six players I will be finding information on.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.