# load heart.csv directly from my website
library(tidyverse)
Heart <- read_csv("https://nmimoto.github.io/datasets/heart.csv")
Heart # shows you a bit of data with some info.## # A tibble: 303 × 15
## index Age Sex ChestPain RestBP Chol Fbs RestECG MaxHR ExAng Oldpeak
## <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 63 1 typical 145 233 1 2 150 0 2.3
## 2 2 67 1 asymptomatic 160 286 0 2 108 1 1.5
## 3 3 67 1 asymptomatic 120 229 0 2 129 1 2.6
## 4 4 37 1 nonanginal 130 250 0 0 187 0 3.5
## 5 5 41 0 nontypical 130 204 0 2 172 0 1.4
## 6 6 56 1 nontypical 120 236 0 0 178 0 0.8
## 7 7 62 0 asymptomatic 140 268 0 2 160 0 3.6
## 8 8 57 0 asymptomatic 120 354 0 0 163 1 0.6
## 9 9 63 1 asymptomatic 130 254 0 2 147 0 1.4
## 10 10 53 1 asymptomatic 140 203 1 2 155 1 3.1
## # ℹ 293 more rows
## # ℹ 4 more variables: Slope <dbl>, Ca <dbl>, Thal <chr>, AHD <chr>
## [1] "index" "Age" "Sex" "ChestPain" "RestBP" "Chol"
## [7] "Fbs" "RestECG" "MaxHR" "ExAng" "Oldpeak" "Slope"
## [13] "Ca" "Thal" "AHD"
## # A tibble: 303 × 15
## index Age Sex ChestPain RestBP Chol Fbs RestECG MaxHR ExAng Oldpeak
## <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 63 1 typical 145 233 1 2 150 0 2.3
## 2 2 67 1 asymptomatic 160 286 0 2 108 1 1.5
## 3 3 67 1 asymptomatic 120 229 0 2 129 1 2.6
## 4 4 37 1 nonanginal 130 250 0 0 187 0 3.5
## 5 5 41 0 nontypical 130 204 0 2 172 0 1.4
## 6 6 56 1 nontypical 120 236 0 0 178 0 0.8
## 7 7 62 0 asymptomatic 140 268 0 2 160 0 3.6
## 8 8 57 0 asymptomatic 120 354 0 0 163 1 0.6
## 9 9 63 1 asymptomatic 130 254 0 2 147 0 1.4
## 10 10 53 1 asymptomatic 140 203 1 2 155 1 3.1
## # ℹ 293 more rows
## # ℹ 4 more variables: Slope <dbl>, Ca <dbl>, Thal <chr>, AHD <chr>
## Rows: 303
## Columns: 15
## $ index <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1…
## $ Age <dbl> 63, 67, 67, 37, 41, 56, 62, 57, 63, 53, 57, 56, 56, 44, 52, …
## $ Sex <dbl> 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, …
## $ ChestPain <chr> "typical", "asymptomatic", "asymptomatic", "nonanginal", "no…
## $ RestBP <dbl> 145, 160, 120, 130, 130, 120, 140, 120, 130, 140, 140, 140, …
## $ Chol <dbl> 233, 286, 229, 250, 204, 236, 268, 354, 254, 203, 192, 294, …
## $ Fbs <dbl> 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, …
## $ RestECG <dbl> 2, 2, 2, 0, 2, 0, 2, 0, 2, 2, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, …
## $ MaxHR <dbl> 150, 108, 129, 187, 172, 178, 160, 163, 147, 155, 148, 153, …
## $ ExAng <dbl> 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, …
## $ Oldpeak <dbl> 2.3, 1.5, 2.6, 3.5, 1.4, 0.8, 3.6, 0.6, 1.4, 3.1, 0.4, 1.3, …
## $ Slope <dbl> 3, 2, 2, 3, 1, 1, 3, 1, 2, 3, 2, 2, 2, 1, 1, 1, 3, 1, 1, 1, …
## $ Ca <dbl> 0, 3, 2, 0, 0, 0, 2, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, …
## $ Thal <chr> "fixed", "normal", "reversable", "normal", "normal", "normal…
## $ AHD <chr> "No", "Yes", "Yes", "No", "No", "No", "Yes", "No", "Yes", "Y…
# Make column Age as integer class (quotes are necessary here)
Heart2 = Heart %>% mutate_at("Age", as.integer)
# It is a good practice to use different name when
# manipulating a DF so that you don’t overwrite the original.
Heart2 # look at the class of Age## # A tibble: 303 × 15
## index Age Sex ChestPain RestBP Chol Fbs RestECG MaxHR ExAng Oldpeak
## <dbl> <int> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 63 1 typical 145 233 1 2 150 0 2.3
## 2 2 67 1 asymptomatic 160 286 0 2 108 1 1.5
## 3 3 67 1 asymptomatic 120 229 0 2 129 1 2.6
## 4 4 37 1 nonanginal 130 250 0 0 187 0 3.5
## 5 5 41 0 nontypical 130 204 0 2 172 0 1.4
## 6 6 56 1 nontypical 120 236 0 0 178 0 0.8
## 7 7 62 0 asymptomatic 140 268 0 2 160 0 3.6
## 8 8 57 0 asymptomatic 120 354 0 0 163 1 0.6
## 9 9 63 1 asymptomatic 130 254 0 2 147 0 1.4
## 10 10 53 1 asymptomatic 140 203 1 2 155 1 3.1
## # ℹ 293 more rows
## # ℹ 4 more variables: Slope <dbl>, Ca <dbl>, Thal <chr>, AHD <chr>
# Replace 1 = Male and 0 = Female in column Sex
Heart3 = Heart2 %>% mutate(Sex = case_when(Sex==1 ~ "Male",
Sex==0 ~ "Female",
.default = NA))
# The last line (.default = NA) means that if there’s anything other than
# 1 or 0 in Sex column, then it should be replaced with NA.
# This is a safeguard.
Heart3## # A tibble: 303 × 15
## index Age Sex ChestPain RestBP Chol Fbs RestECG MaxHR ExAng Oldpeak
## <dbl> <int> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 63 Male typical 145 233 1 2 150 0 2.3
## 2 2 67 Male asymptomat… 160 286 0 2 108 1 1.5
## 3 3 67 Male asymptomat… 120 229 0 2 129 1 2.6
## 4 4 37 Male nonanginal 130 250 0 0 187 0 3.5
## 5 5 41 Female nontypical 130 204 0 2 172 0 1.4
## 6 6 56 Male nontypical 120 236 0 0 178 0 0.8
## 7 7 62 Female asymptomat… 140 268 0 2 160 0 3.6
## 8 8 57 Female asymptomat… 120 354 0 0 163 1 0.6
## 9 9 63 Male asymptomat… 130 254 0 2 147 0 1.4
## 10 10 53 Male asymptomat… 140 203 1 2 155 1 3.1
## # ℹ 293 more rows
## # ℹ 4 more variables: Slope <dbl>, Ca <dbl>, Thal <chr>, AHD <chr>
## # A tibble: 303 × 15
## index Age Sex ChestPain RestBP Chol Fbs RestECG MaxHR ExAng Oldpeak
## <dbl> <int> <fct> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 63 Male typical 145 233 1 2 150 0 2.3
## 2 2 67 Male asymptomat… 160 286 0 2 108 1 1.5
## 3 3 67 Male asymptomat… 120 229 0 2 129 1 2.6
## 4 4 37 Male nonanginal 130 250 0 0 187 0 3.5
## 5 5 41 Female nontypical 130 204 0 2 172 0 1.4
## 6 6 56 Male nontypical 120 236 0 0 178 0 0.8
## 7 7 62 Female asymptomat… 140 268 0 2 160 0 3.6
## 8 8 57 Female asymptomat… 120 354 0 0 163 1 0.6
## 9 9 63 Male asymptomat… 130 254 0 2 147 0 1.4
## 10 10 53 Male asymptomat… 140 203 1 2 155 1 3.1
## # ℹ 293 more rows
## # ℹ 4 more variables: Slope <dbl>, Ca <dbl>, Thal <chr>, AHD <chr>
## Sex
## Female Male
## 97 206
## Sex
## Female Male
## 97 206
# Change order of factor levels
Heart4 = Heart3 %>%
mutate_at("Sex", function(x) fct_relevel(x, c("Male", "Female")))
# Now it is Male/Female, instead of Female/Male.
Heart4 %>% select(Sex) %>% table## Sex
## Male Female
## 206 97
## # A tibble: 303 × 6
## ExAng Oldpeak Slope Ca Thal AHD
## <dbl> <dbl> <dbl> <dbl> <chr> <chr>
## 1 0 2.3 3 0 fixed No
## 2 1 1.5 2 3 normal Yes
## 3 1 2.6 2 2 reversable Yes
## 4 0 3.5 3 0 normal No
## 5 0 1.4 1 0 normal No
## 6 0 0.8 1 0 normal No
## 7 0 3.6 3 2 normal Yes
## 8 1 0.6 1 0 normal No
## 9 0 1.4 2 1 reversable Yes
## 10 1 3.1 3 0 reversable Yes
## # ℹ 293 more rows
# Now make the two columns into factor
Heart5 = Heart3 %>% mutate_at(c("Thal", "AHD"), as.factor)
Heart5## # A tibble: 303 × 15
## index Age Sex ChestPain RestBP Chol Fbs RestECG MaxHR ExAng Oldpeak
## <dbl> <int> <fct> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 63 Male typical 145 233 1 2 150 0 2.3
## 2 2 67 Male asymptomat… 160 286 0 2 108 1 1.5
## 3 3 67 Male asymptomat… 120 229 0 2 129 1 2.6
## 4 4 37 Male nonanginal 130 250 0 0 187 0 3.5
## 5 5 41 Female nontypical 130 204 0 2 172 0 1.4
## 6 6 56 Male nontypical 120 236 0 0 178 0 0.8
## 7 7 62 Female asymptomat… 140 268 0 2 160 0 3.6
## 8 8 57 Female asymptomat… 120 354 0 0 163 1 0.6
## 9 9 63 Male asymptomat… 130 254 0 2 147 0 1.4
## 10 10 53 Male asymptomat… 140 203 1 2 155 1 3.1
## # ℹ 293 more rows
## # ℹ 4 more variables: Slope <dbl>, Ca <dbl>, Thal <fct>, AHD <fct>
## # A tibble: 303 × 6
## ExAng Oldpeak Slope Ca Thal AHD
## <dbl> <dbl> <dbl> <dbl> <fct> <fct>
## 1 0 2.3 3 0 fixed No
## 2 1 1.5 2 3 normal Yes
## 3 1 2.6 2 2 reversable Yes
## 4 0 3.5 3 0 normal No
## 5 0 1.4 1 0 normal No
## 6 0 0.8 1 0 normal No
## 7 0 3.6 3 2 normal Yes
## 8 1 0.6 1 0 normal No
## 9 0 1.4 2 1 reversable Yes
## 10 1 3.1 3 0 reversable Yes
## # ℹ 293 more rows
## Thal
## fixed normal reversable
## 18 166 117
## AHD
## No Yes
## 164 139
## # A tibble: 303 × 14
## Age Sex ChestPain RestBP Chol Fbs RestECG MaxHR ExAng Oldpeak Slope
## <int> <fct> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 63 Male typical 145 233 1 2 150 0 2.3 3
## 2 67 Male asymptomat… 160 286 0 2 108 1 1.5 2
## 3 67 Male asymptomat… 120 229 0 2 129 1 2.6 2
## 4 37 Male nonanginal 130 250 0 0 187 0 3.5 3
## 5 41 Female nontypical 130 204 0 2 172 0 1.4 1
## 6 56 Male nontypical 120 236 0 0 178 0 0.8 1
## 7 62 Female asymptomat… 140 268 0 2 160 0 3.6 3
## 8 57 Female asymptomat… 120 354 0 0 163 1 0.6 1
## 9 63 Male asymptomat… 130 254 0 2 147 0 1.4 2
## 10 53 Male asymptomat… 140 203 1 2 155 1 3.1 3
## # ℹ 293 more rows
## # ℹ 3 more variables: Ca <dbl>, Thal <fct>, AHD <fct>
## # A tibble: 303 × 12
## Sex ChestPain RestBP Fbs RestECG MaxHR ExAng Oldpeak Slope Ca Thal
## <fct> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <fct>
## 1 Male typical 145 1 2 150 0 2.3 3 0 fixed
## 2 Male asymptomat… 160 0 2 108 1 1.5 2 3 norm…
## 3 Male asymptomat… 120 0 2 129 1 2.6 2 2 reve…
## 4 Male nonanginal 130 0 0 187 0 3.5 3 0 norm…
## 5 Female nontypical 130 0 2 172 0 1.4 1 0 norm…
## 6 Male nontypical 120 0 0 178 0 0.8 1 0 norm…
## 7 Female asymptomat… 140 0 2 160 0 3.6 3 2 norm…
## 8 Female asymptomat… 120 0 0 163 1 0.6 1 0 norm…
## 9 Male asymptomat… 130 0 2 147 0 1.4 2 1 reve…
## 10 Male asymptomat… 140 1 2 155 1 3.1 3 0 reve…
## # ℹ 293 more rows
## # ℹ 1 more variable: AHD <fct>
## # A tibble: 303 × 3
## RestBP Age Chol
## <dbl> <int> <dbl>
## 1 145 63 233
## 2 160 67 286
## 3 120 67 229
## 4 130 37 250
## 5 130 41 204
## 6 120 56 236
## 7 140 62 268
## 8 120 57 354
## 9 130 63 254
## 10 140 53 203
## # ℹ 293 more rows
##------------------------------
## 1. Numeric to Integer (Age)
# Make column Age as integer class (quotes are necessary here)
Heart2 = Heart %>% mutate_at("Age", as.integer)
# It is a good practice to use different name when
# manipulating a DF so that you don’t overwrite the original.
Heart2 # look at the class of Age
##------------------------------
## 2. Check Class
# Check for class
Heart
# This will show all the columns and their class
glimpse(Heart)
##------------------------------
## 3. Numeric to Integer (Age)
# Make column Age as integer class (quotes are necessary here)
Heart2 = Heart %>% mutate_at("Age", as.integer)
# It is a good practice to use different name when
# manipulating a DF so that you don’t overwrite the original.
Heart2 # look at the class of Age
##------------------------------
## 4. Numeric to Factor (Sex)
# Replace 1 = Male and 0 = Female in column Sex
Heart3 = Heart2 %>% mutate(Sex = case_when(Sex==1 ~ "Male",
Sex==0 ~ "Female",
.default = NA))
# The last line (.default = NA) means that if there’s anything other than
# 1 or 0 in Sex column, then it should be replaced with NA.
# This is a safeguard.
Heart3
# Now make the column into factor
Heart3 = Heart3 %>% mutate_at("Sex", as.factor)
Heart3
# check
Heart3 %>% select(Sex) %>% table
##------------------------------
## 5. Order of factor levels (Sex)
# Levels are Female / Male
Heart3 %>% select(Sex) %>% table
# Change order of factor levels
Heart4 = Heart3 %>%
mutate_at("Sex", function(x) fct_relevel(x, c("Male", "Female")))
# Now it is Male/Female, instead of Female/Male.
Heart4 %>% select(Sex) %>% table
##------------------------------
## 6. Character to Factor (Sex)
# Check Thal and AHD
Heart3[10:15]
# Now make the two columns into factor
Heart5 = Heart3 %>% mutate_at(c("Thal", "AHD"), as.factor)
Heart5
# check
Heart5[10:15]
Heart5 %>% select(Thal) %>% table
Heart5 %>% select(AHD) %>% table
##------------------------------
## 7. Selecting and Omitting Multiple Columns
Heart5 %>% select(-index) # remove index
Heart5 %>% select(-c(index, Age, Chol) # remove index, Age, Chol
Heart5 %>% select(c(RestBP, Age, Chol)) # select RestBP, Age, Chol