Assignment 1 - Basic Data Loading and Transformations

Introduction

This report analyzes the UCI Heart Disease dataset by performing basic data loading and transformation tasks using R. The dataset contains demographic and cardiovascular health information from 303 patients and is commonly used for educational purposes and predictive modeling of heart disease. The original dataset is available through the UCI Machine Learning Repository: https://archive.ics.uci.edu/dataset/45/heart+disease

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
url <- "https://raw.githubusercontent.com/yeimiperez14/Data_607/main/Assignment-01/heart.csv"

df <- read_csv(file = url, show_col_types = FALSE, progress = FALSE)

glimpse(df)
Rows: 303
Columns: 14
$ age      <dbl> 63, 37, 41, 56, 57, 57, 56, 44, 52, 57, 54, 48, 49, 64, 58, 5…
$ sex      <dbl> 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1…
$ cp       <dbl> 3, 2, 1, 1, 0, 0, 1, 1, 2, 2, 0, 2, 1, 3, 3, 2, 2, 3, 0, 3, 0…
$ trestbps <dbl> 145, 130, 130, 120, 120, 140, 140, 120, 172, 150, 140, 130, 1…
$ chol     <dbl> 233, 250, 204, 236, 354, 192, 294, 263, 199, 168, 239, 275, 2…
$ fbs      <dbl> 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0…
$ restecg  <dbl> 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1…
$ thalach  <dbl> 150, 187, 172, 178, 163, 148, 153, 173, 162, 174, 160, 139, 1…
$ exang    <dbl> 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0…
$ oldpeak  <dbl> 2.3, 3.5, 1.4, 0.8, 0.6, 0.4, 1.3, 0.0, 0.5, 1.6, 1.2, 0.2, 0…
$ slope    <dbl> 0, 0, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 1, 2, 0, 2, 2, 1…
$ ca       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0…
$ thal     <dbl> 1, 2, 2, 2, 2, 1, 2, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3…
$ target   <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…

Data transformation

The original dataset contained 14 variables describing demographic and cardiovascular characteristics. For this assignment, a subset of seven variables was selected to focus on patient demographics, blood pressure, cholesterol, heart rate, exercise-induced angina, and heart disease status. Abbreviated variable names and coded values were also converted into more descriptive labels to improve readability and make the dataset easier to interpret.

heart_selected <- df |>
  select(
    age,
    sex,
    trestbps,
    chol,
    thalach,
    exang,
    target
  )

glimpse(heart_selected)
Rows: 303
Columns: 7
$ age      <dbl> 63, 37, 41, 56, 57, 57, 56, 44, 52, 57, 54, 48, 49, 64, 58, 5…
$ sex      <dbl> 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1…
$ trestbps <dbl> 145, 130, 130, 120, 120, 140, 140, 120, 172, 150, 140, 130, 1…
$ chol     <dbl> 233, 250, 204, 236, 354, 192, 294, 263, 199, 168, 239, 275, 2…
$ thalach  <dbl> 150, 187, 172, 178, 163, 148, 153, 173, 162, 174, 160, 139, 1…
$ exang    <dbl> 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0…
$ target   <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…

Conclusion

This assignment demonstrated how to import a dataset from a reproducible online source, select relevant variables, rename abbreviated column names, and convert coded values into meaningful labels using R. These transformations improve the readability and usability of the dataset while preserving the original information. Future work could include exploratory data analysis, data visualization, and predictive modeling to better understand factors associated with heart disease.

heart_clean <- heart_selected |>
  rename(
    resting_blood_pressure = trestbps,
    cholesterol = chol,
    maximum_heart_rate = thalach,
    exercise_induced_angina = exang,
    heart_disease = target
  )

glimpse(heart_clean)
Rows: 303
Columns: 7
$ age                     <dbl> 63, 37, 41, 56, 57, 57, 56, 44, 52, 57, 54, 48…
$ sex                     <dbl> 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0…
$ resting_blood_pressure  <dbl> 145, 130, 130, 120, 120, 140, 140, 120, 172, 1…
$ cholesterol             <dbl> 233, 250, 204, 236, 354, 192, 294, 263, 199, 1…
$ maximum_heart_rate      <dbl> 150, 187, 172, 178, 163, 148, 153, 173, 162, 1…
$ exercise_induced_angina <dbl> 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0…
$ heart_disease           <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
heart_clean <- heart_clean |>
  mutate(
    sex = if_else(sex == 1, "Male", "Female"),
    
    exercise_induced_angina =
      if_else(exercise_induced_angina == 1,
              "Yes",
              "No"),
    
    heart_disease =
      if_else(heart_disease == 1,
              "Heart Disease",
              "No Heart Disease")
  )

glimpse(heart_clean)
Rows: 303
Columns: 7
$ age                     <dbl> 63, 37, 41, 56, 57, 57, 56, 44, 52, 57, 54, 48…
$ sex                     <chr> "Male", "Male", "Female", "Male", "Female", "M…
$ resting_blood_pressure  <dbl> 145, 130, 130, 120, 120, 140, 140, 120, 172, 1…
$ cholesterol             <dbl> 233, 250, 204, 236, 354, 192, 294, 263, 199, 1…
$ maximum_heart_rate      <dbl> 150, 187, 172, 178, 163, 148, 153, 173, 162, 1…
$ exercise_induced_angina <chr> "No", "No", "No", "No", "Yes", "No", "No", "No…
$ heart_disease           <chr> "Heart Disease", "Heart Disease", "Heart Disea…