-
Notifications
You must be signed in to change notification settings - Fork 0
/
pfxModifyAtbatData.R
executable file
·86 lines (60 loc) · 1.8 KB
/
pfxModifyAtbatData.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
# Bartev Vartanian
# 10/17/11
#
# Modify atbat data
# Use pa for plate appearance (a la Understanding Sabermetrics, Gabriel Costa)
# Function definition in another file
# ModifyAtBatCols(atbat)
source('pfxFuncModifyAtBatCols.R')
# From PfxLoad09Data.R
load("pfxDataSample.Rdat")
# This function in vectorized form is performed by ModifyAtBatCols
# Add runs, and current score
# This function is in pfxCumRunsFunction.R
# source("pfxCumRunsFunction.R")
# pa <- RunsScoredAtBat(pa)
# Save data
save(pa, file = "pfxData-plateAppearance.Rdat")
# TRY STUFF HERE
# visitor bats first (top of the inning)
pps <- pp[1:25,]
pas <- pa[1:25,]
# dpp <- sqldf('select game_id, upid, ubnum, num, count from pps where finalPitch = "TRUE"')
# dpp <- sqldf('select * from pps where finalPitch')
# dpa <- sqldf('select game_id, ubnum, half, htRuns, atRuns from pas')
# dm <- merge(dpp, dpa, by = 'ubnum', all.x = TRUE)
# dmm <- merge(pps, dm, by = intersect(names(pps), names(dm)), all.x = TRUE, sort = FALSE)
head(pas)
head(pps)
matchCols <- c('game_id', 'batter', 'pitcher', 'ubnum')
ppst <- subset(pps, finalPitch == TRUE)
mg <- merge(ppst, pas, by = c('ubnum', 'game_id', 'pitcher', 'batter', 'num'), all.x = FALSE, sort = FALSE)
mg
intNames <- intersect(names(pps), names(mg))
intNames
mgg <- merge(pps, mg, by = intersect(names(pps), names(mg)), all.x = TRUE, sort = FALSE)
mgg <- mgg[with(mgg, order(seasonPitchNum))]
mgg.sort <- mgg[order(mgg$seasonPitchNum), ]
mgg.sort
upidmgg <- mgg$upid
upidmgg
sort(mg, by ~ upid)
mg[with(mg, order(upid))]
ppst
mgg <- merge(pps, ppst, by = intersect(names(pps), names(ppst)), all.x = TRUE, sort = FALSE)
mgg
plot(mgg$id)
dpp
pps[1:13, ]
head(pps)
head(dpp)
dpa
names(pps)
names(pas)
str(pps)
str(pas)
# dm <- sqldf('select * from dpp inner join dpa')
head(dpp)
dm
head(dm, 20)
head(pps)