-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path1-clean-data.Rmd
68 lines (49 loc) · 1.41 KB
/
1-clean-data.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
---
title: "Clean data"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Import data
```{r import_data}
data_raw_dir = "data-raw/"
filename = "processed.cleveland.csv"
full_file = paste0(data_raw_dir, filename)
if (!file.exists(full_file)) {
url = "http://archive.ics.uci.edu/ml/machine-learning-databases/heart-disease/processed.cleveland.data"
# Load the remote data.
data = rio::import(url, header = FALSE, format = "csv", na.strings = '?')
# Update column names.
names(data) = c("age", "sex", "cp", "trestbps", "chol","fbs", "restecg",
"thalach","exang", "oldpeak","slope", "ca", "thal", "num")
# Save to local directory.
rio::export(data, file = full_file)
} else {
data = rio::import(full_file)
}
```
## Categoricals to factors
```{r categoricals_to_factors}
# Explicitly define certain variables as factors rather than numbers.
data = ck37r::categoricals_to_factors(data,
categoricals = c("ca", "cp", "slope", "thal"),
verbose = TRUE)
str(data)
```
## Save unimputed version
```{r save_unimputed}
save(data, file = "data/clean-data-unimputed.RData")
```
## Missing values
```{r missing_values}
# We have a few missing values.
colSums(is.na(data))
# Omit missing values for now.
data = na.omit(data)
colSums(is.na(data))
```
## Save result
```{r save_data}
save(data, file = "data/clean-data-imputed.RData")
```