-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_baseref_cleanup.Rmd
289 lines (203 loc) · 6.08 KB
/
3_baseref_cleanup.Rmd
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
---
title: "R Notebook"
output: html_notebook
---
# Introduction
This notebook is dedicated to cleaning, merging, and analyzing baseball data from two datasets. It includes detailed steps to clean data inconsistencies, merge datasets based on player names, seasons, and teams, and address data discrepancies by normalizing names.
# Initial Setup
## Load Libraries
```{r}
library(dplyr)
library(stringr)
library(stringi)
data <- read.csv("dataset/data1.csv", stringsAsFactors = FALSE)
nrow(data)
```
# Data Cleaning
## Load and Inspect Data 1
We start by loading the first dataset and inspecting its initial state.
```{r}
head(data)
```
## Clean Data 1
We perform several cleaning steps including removing invalid positions, correcting encoded characters, and standardizing player names.
```{r}
data_removed <- data[is.na(data$Pos) | data$Pos == "" | data$Pos == "Pos", ]
data <- data[!is.na(data$Pos) & data$Pos != "" & data$Pos != "Pos", ]
data$Name <- gsub("\\*$", "", data$Name)
data$Name <- gsub("√©", "é", data$Name)
data$Name <- gsub("√≥", "ó", data$Name)
data$Name <- gsub("√±", "ñ", data$Name)
data$Name <- stri_trans_general(data$Name, "Latin-ASCII")
```
```{r}
data_removed
```
```{r}
nrow(data)
```
```{r}
head(data)
```
```{r}
print_non_alphanumeric_names <- function(data) {
non_alphanumeric_players <- data %>%
filter(!grepl("^[a-zA-Z0-9 ]+$", Name)) %>%
select(Name)
if (nrow(non_alphanumeric_players) > 0) {
print(non_alphanumeric_players)
} else {
print("No players with non-alphanumeric or space characters in their names.")
}
}
print_non_alphanumeric_names(data)
```
```{r}
clean_player_names <- function(data) {
data$Name <- str_replace_all(data$Name, "#$", "")
data$Name <- str_replace_all(data$Name, "[^a-zA-Z0-9 ]", " ")
return(data)
}
data <- clean_player_names(data)
```
```{r}
print_non_alphanumeric_names(data)
```
```{r}
write.csv(data, "dataset/data1.csv", row.names = FALSE)
```
## Clean Data 2
Similar cleaning procedures are applied to the second dataset.
```{r}
data <- read.csv("dataset/data2.csv", stringsAsFactors = FALSE)
head(data)
```
```{r}
nrow(data)
```
```{r}
data_removed <- data[is.na(data$Age) | data$Age == "" | data$Age == "Age", ]
data <- data[!is.na(data$Age) & data$Age != "" & data$Age != "Age", ]
data$Name <- gsub("\\*$", "", data$Name)
data$Name <- gsub("√©", "é", data$Name)
data$Name <- gsub("√≥", "ó", data$Name)
data$Name <- gsub("√±", "ñ", data$Name)
data$Name <- stri_trans_general(data$Name, "Latin-ASCII")
```
```{r}
data_removed
```
```{r}
data
```
```{r}
print_non_alphanumeric_names(data)
```
```{r}
data <- clean_player_names(data)
```
```{r}
print_non_alphanumeric_names(data)
```
```{r}
nrow(data)
```
```{r}
write.csv(data, "dataset/data2.csv", row.names = FALSE)
```
# Merging Data
## Load and Merge Data
We perform a full join on the two cleaned datasets based on player names, seasons, and teams.
```{r}
library(dplyr)
data1 <- read.csv("dataset/data1.csv", stringsAsFactors = FALSE)
data2 <- read.csv("dataset/data2.csv", stringsAsFactors = FALSE)
data1 <- dplyr::mutate(data1, source = "data1")
data2 <- dplyr::mutate(data2, source = "data2")
full_join_result <- dplyr::full_join(data1, data2, by = c("Name", "Season", "Team"))
unmatched_data1 <- full_join_result %>%
dplyr::filter(is.na(source.y)) %>%
dplyr::select(names(data1)[names(data1) %in% names(full_join_result)])
unmatched_data2 <- full_join_result %>%
dplyr::filter(is.na(source.x)) %>%
dplyr::select(names(data2)[names(data2) %in% names(full_join_result)])
print("Rows in Data1 that don't have a match in Data2:")
print(unmatched_data1)
```
```{r}
print("Rows in Data2 that don't have a match in Data1:")
print(unmatched_data2)
```
## Removing trailing HOF from Player Names
```{r}
data1 <- read.csv("dataset/data1.csv", stringsAsFactors = FALSE)
data2 <- read.csv("dataset/data2.csv", stringsAsFactors = FALSE)
data2$Name <- str_trim(stringr::str_replace(data2$Name, "\\s+HOF$", ""))
data1 <- mutate(data1, source = "data1")
data2 <- mutate(data2, source = "data2")
full_join_result <- full_join(data1, data2, by = c("Name", "Season", "Team"))
```
## Identify Unmatched Records
```{r}
unmatched_data1 <- full_join_result %>%
filter(is.na(source.y)) %>%
select(names(data1)[names(data1) %in% names(full_join_result)]) # Safely select matching columns
unmatched_data2 <- full_join_result %>%
filter(is.na(source.x)) %>%
select(names(data2)[names(data2) %in% names(full_join_result)]) # Safely select matching columns
```
```{r}
print("Rows in Data1 that don't have a match in Data2:")
print(unmatched_data1)
```
```{r}
print("Rows in Data2 that don't have a match in Data1:")
print(unmatched_data2)
```
We have identified the error as the trailing "jr" in data1
```{r}
print(unmatched_data1[grepl("Jr", unmatched_data1$Name), ])
print(unmatched_data2[grepl("Jr", unmatched_data2$Name), ])
```
## Normalize and Finalize Merging
Normalization of player names is performed to ensure better matching during the join.
```{r}
normalize_name <- function(name) {
name <- stringr::str_trim(name)
name <- gsub("\\s+", " ", name)
name <- tolower(name)
name <- gsub("\\bjr\\.?$", "jr", name, perl = TRUE)
return(name)
}
data1$Name <- sapply(data1$Name, normalize_name)
data2$Name <- sapply(data2$Name, normalize_name)
full_join_result <- full_join(data1, data2, by = c("Name", "Season", "Team"))
unmatched_data1 <- full_join_result %>%
filter(is.na(source.y)) %>%
select(names(data1)[names(data1) %in% names(full_join_result)])
unmatched_data2 <- full_join_result %>%
filter(is.na(source.x)) %>%
select(names(data2)[names(data2) %in% names(full_join_result)])
```
```{r}
print("Rows in Data1 that don't have a match in Data2:")
print(unmatched_data1)
```
```{r}
print("Rows in Data2 that don't have a match in Data1:")
print(unmatched_data2)
```
## Results and Data Export
Finally, we inspect the merged results and export the data.
```{r}
head(full_join_result)
```
```{r}
summary(full_join_result)
```
```{r}
nrow(full_join_result)
```
```{r}
write.csv(full_join_result, "dataset/merged_data.csv", row.names = FALSE)
```