knitr::opts_chunk$set(echo = TRUE)

1 Section A – Conditionals and Simple Logic

1.1 Problem 1: Evaluate Glucose Level

fbg <- 112

if (fbg < 100) {
  print("Normal")
} else if (fbg >= 100 & fbg <= 125) {
  print("Prediabetes")
} else {
  print("Diabetes")
}
## [1] "Prediabetes"

1.2 Problem 2: Check for Fever

body_temp <- 38.2

if (body_temp <= 37.5) {
  print("Normal Temperature")
} else {
  print("Fever Detected")
}
## [1] "Fever Detected"

1.3 Problem 3: Problem 3: BMI Classification

bmi <- 29.5
# Because no BMI classification range was given. I Googled it and used the following range
# BMI <18.5 Underweight, 18.5 to 24.9 Normal weight, 25.0 to 29.9 Overweight, and 30.0 and above Obesity

if (bmi <= 18.5) {
  print("Underweight")
} else if (bmi >= 18.5 & bmi <= 24.9) {
  print("Normal weight")
} else if (bmi >= 25 & bmi <=29.9) {
  print("Overweight")
} else {
  print("Obese")
}
## [1] "Overweight"

1.4 Problem 4: : Lab Result Flagging

hb <- 10.5

if (hb <= 10.5) {
  print("Anemia")
} else {
  print("Normal")
}
## [1] "Anemia"

1.5 Problem 5: Problem 5: Evaluate Cholesterol Panel

ldl <- 165

if (ldl < 130) {
  print("Optimal")
} else if (ldl >=130 & ldl <= 159) {
  print ("Borderline")
} else {
  print("High")
}
## [1] "High"

2 Section B – Conditionals with Data Frames

2.1 Problem 6: Create a Clinical Dataset

# Creating my dataframe
clinic_df <- data.frame(
  Name = c("Aisha", "Rahman", "Rima", "Hossain"),
  Age = c(45, 52, 37, 60),
  BMI = c(31.2, 28.5, 24.1, 33.4),
  BP_Systolic = c(150, 165, 120, 175)
)

print (clinic_df)
##      Name Age  BMI BP_Systolic
## 1   Aisha  45 31.2         150
## 2  Rahman  52 28.5         165
## 3    Rima  37 24.1         120
## 4 Hossain  60 33.4         175

2.2 Problem 7: Problem 7: Add a Hypertension Column to the Problem 6 dataframe

clinic_df$hypertensive <- ifelse(clinic_df$BP_Systolic >= 140, "Yes", "No")
print(clinic_df)
##      Name Age  BMI BP_Systolic hypertensive
## 1   Aisha  45 31.2         150          Yes
## 2  Rahman  52 28.5         165          Yes
## 3    Rima  37 24.1         120           No
## 4 Hossain  60 33.4         175          Yes

2.3 Problem 8: Problem 8: Add an Obesity Status Column to the Problem 6 dataframe

Background: • “Obese” if BMI ≥30 • “Overweight” if BMI 25–29.9 • “Normal” if BMI <25 Task: Use nested ifelse() to classify patients and store in a new column “BMI_Status”.

Create a Gene Expression Matrix ypes

clinic_df$bmi_status <- ifelse(clinic_df$BMI >= 30, "Obese", ifelse(clinic_df$BMI >=25 & clinic_df$BMI <= 29.9, "Overweight", "Normal"))
print(clinic_df)
##      Name Age  BMI BP_Systolic hypertensive bmi_status
## 1   Aisha  45 31.2         150          Yes      Obese
## 2  Rahman  52 28.5         165          Yes Overweight
## 3    Rima  37 24.1         120           No     Normal
## 4 Hossain  60 33.4         175          Yes      Obese

2.4 Problem 9: Identify Elderly Patients in clinic_data

elderly <-clinic_df[clinic_df$Age >= 50, ]

print(elderly)
##      Name Age  BMI BP_Systolic hypertensive bmi_status
## 2  Rahman  52 28.5         165          Yes Overweight
## 4 Hossain  60 33.4         175          Yes      Obese

2.5 Problem 10: Assign Risk Level in clinic_data

clinic_df$risk_level <- ifelse(clinic_df$Age > 50 & clinic_df$hypertensive =="Yes", "High Risk", ifelse(clinic_df$Age > 50 | clinic_df$hypertensive =="Yes", "Moderate Risk", "Low Risk"))
print(clinic_df)
##      Name Age  BMI BP_Systolic hypertensive bmi_status    risk_level
## 1   Aisha  45 31.2         150          Yes      Obese Moderate Risk
## 2  Rahman  52 28.5         165          Yes Overweight     High Risk
## 3    Rima  37 24.1         120           No     Normal      Low Risk
## 4 Hossain  60 33.4         175          Yes      Obese     High Risk

3 Section C – Loops and Iteration

3.1 Problem 11: Daily Data Entry Reminder

x <- c("Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
for (i in 1: length(x)) {
  print(paste("Please upload lab data for", x[i], sep = " "))
}
## [1] "Please upload lab data for Saturday"
## [1] "Please upload lab data for Sunday"
## [1] "Please upload lab data for Monday"
## [1] "Please upload lab data for Tuesday"
## [1] "Please upload lab data for Wednesday"
## [1] "Please upload lab data for Thursday"
## [1] "Please upload lab data for Friday"

3.2 Problem 12: Sequential Year Tracker

Background: The lab wants a yearly log for 2015 to 2025. Task: Use a for loop to print “Processing year: ” for each year in that range.

for (i in 2015:2025) {
  print(paste("Processing year", i, sep = " "))
}
## [1] "Processing year 2015"
## [1] "Processing year 2016"
## [1] "Processing year 2017"
## [1] "Processing year 2018"
## [1] "Processing year 2019"
## [1] "Processing year 2020"
## [1] "Processing year 2021"
## [1] "Processing year 2022"
## [1] "Processing year 2023"
## [1] "Processing year 2024"
## [1] "Processing year 2025"

3.3 Problem 13: Viral Load Tracking

Background: You have a vector of viral loads (copies/mL): c(). Task: Use a for loop and conditional logic: • Print “Normal” if < 1000 • “Elevated” if between 1000–9999 • “Critical” if ≥ 10000

viral_load <- c(100, 550, 1200, 20000, 850)

for (i in 1:5) {
  if(viral_load[i] < 1000) {
    print("Normal")
  } else if (viral_load[i] >= 1000 & viral_load[i] <= 9999){
    print("Elevated")
  } else {
    print ("Critical")
  }
}
## [1] "Normal"
## [1] "Normal"
## [1] "Elevated"
## [1] "Critical"
## [1] "Normal"

4 Section D - Medium to Hard

4.1 Problem 14: Automated Clinical Categorization

#Creating my dataframe
hiv <- data.frame(
  Name = c("Rahim", "Sumaiya", "Babul", "Joya"),
  CD4_Count = c(120, 480, 230, 700)
)

print(hiv)
##      Name CD4_Count
## 1   Rahim       120
## 2 Sumaiya       480
## 3   Babul       230
## 4    Joya       700
# Adding immunity_status
hiv$immunity_status <- ifelse(hiv$CD4_Count < 200, "Severe Immunodeficiency", ifelse(hiv$CD4_Count >= 200 & hiv$CD4_Count <= 500, "Moderate", "Normal"))

print(hiv)
##      Name CD4_Count         immunity_status
## 1   Rahim       120 Severe Immunodeficiency
## 2 Sumaiya       480                Moderate
## 3   Babul       230                Moderate
## 4    Joya       700                  Normal
#Extracting severe patient
severe <- hiv[hiv$immunity_status == "Severe Immunodeficiency", ]

print(severe)
##    Name CD4_Count         immunity_status
## 1 Rahim       120 Severe Immunodeficiency

4.2 Problem 15: Integrating Loops and Conditionals for Clinical Scoring

# Creating my data as vector
infection_score <- c(10, 35, 50, 80, 95)

# To generate a loop for new condition we need to generate an empty vector where new data generated by fulfiling the condition will be stored.
severity_label <- c()

# Now the "For-loop"
for (i in 1:length(infection_score)) {
  if (infection_score[i] < 30) {
    severity_label[i] <- "Mild"
  } else if (infection_score[i] < 70) {
    severity_label[i] <- "Moderate"
  } else {
    severity_label[i] <- "Severe"
  }
}

# Finally combine infection_score and severity_label data
infection_df <- data.frame(infection_score, severity_label)
print(infection_df)
##   infection_score severity_label
## 1              10           Mild
## 2              35       Moderate
## 3              50       Moderate
## 4              80         Severe
## 5              95         Severe

5 Problem 16: Combine All Skills – Hospital Ward Analysis

# Creating my dataframe
ward <- data.frame(
  Name = c("Mina", "Rafi", "Sima", "Rony", "Asha", "Nayeem"),
  Age = c(25, 50, 61, 45, 70, 58),
  Temp = c(36.8, 38.5, 39.2, 37.0, 38.0, 36.5),
  SpO2 = c(99, 95, 91, 98, 89, 97)
)

print(ward)
##     Name Age Temp SpO2
## 1   Mina  25 36.8   99
## 2   Rafi  50 38.5   95
## 3   Sima  61 39.2   91
## 4   Rony  45 37.0   98
## 5   Asha  70 38.0   89
## 6 Nayeem  58 36.5   97
# Creating an empty column in the loop and setup my conditions
ward$Condition <- c()

for (i in 1:length(ward)) {
  
  if (ward$Temp[i] > 38 & ward$SpO2[i] < 94) {
    ward$Condition[i] <- "Critical"
    
  } else if (ward$Temp[i] > 37 & ward$SpO2[i] < 96) {
    ward$Condition[i] <- "At Risk"
    
  } else {
    ward$Condition[i] <- "Stable"
  }
}

print(ward)
##     Name Age Temp SpO2 Condition
## 1   Mina  25 36.8   99    Stable
## 2   Rafi  50 38.5   95   At Risk
## 3   Sima  61 39.2   91  Critical
## 4   Rony  45 37.0   98    Stable
## 5   Asha  70 38.0   89    Stable
## 6 Nayeem  58 36.5   97    Stable
#Summary table according to condition
summary <- table(ward$Condition)

print(summary)
## 
##  At Risk Critical   Stable 
##        1        1        4