Section A – Conditionals and Simple Logic

Problem 1: Evaluate Glucose Level

fbg <- 112
if (fbg < 100) {
  result <- "Normal"
} else if (fbg >= 100 & fbg <= 125) {
  result <- "Prediabetes"
} else {
  result <- "Diabetes"
}
cat("Glucose Level Classification:", result, "\n")
## Glucose Level Classification: Prediabetes

Problem 2: Check for Fever

temperature <- 38.2
if (temperature > 37.5) {
  cat("Fever Detected\n")
} else {
  cat("Normal Temperature\n")
}
## Fever Detected

Problem 3: BMI Classification

bmi <- 29.5
if (bmi < 25) {
  bmi_class <- "Normal"
} else if (bmi >= 25 & bmi <= 29.9) {
  bmi_class <- "Overweight"
} else {
  bmi_class <- "Obese"
}
cat("BMI Classification:", bmi_class, "\n")
## BMI Classification: Overweight

Problem 4: Lab Result Flagging

hb <- 10.5
if (hb < 12) {
  cat("Anemia\n")
} else {
  cat("Normal\n")
}
## Anemia

Problem 5: Evaluate Cholesterol Panel

ldl <- 165
if (ldl < 130) {
  chol_status <- "Optimal"
} else if (ldl >= 130 & ldl <= 159) {
  chol_status <- "Borderline"
} else {
  chol_status <- "High"
}
cat("Cholesterol Status:", chol_status, "\n")
## Cholesterol Status: High

Section B – Conditionals with Data Frames

Problem 6: Create a Clinical Dataset

patients <- 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),
  stringsAsFactors = FALSE
)
print(patients)
##      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

Problem 7: Add a Hypertension Column

patients$Hypertensive <- ifelse(patients$BP_Systolic >= 140, "Yes", "No")
print(patients)
##      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

Problem 8: Add an Obesity Status Column

patients$BMI_Status <- ifelse(patients$BMI >= 30, "Obese",
                              ifelse(patients$BMI >= 25 & patients$BMI <= 29.9, "Overweight", "Normal"))
print(patients)
##      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

Problem 9: Identify Elderly Patients

elderly_patients <- subset(patients, Age >= 50)
print(elderly_patients)
##      Name Age  BMI BP_Systolic Hypertensive BMI_Status
## 2  Rahman  52 28.5         165          Yes Overweight
## 4 Hossain  60 33.4         175          Yes      Obese

Problem 10: Assign Risk Level

# Count how many risk conditions are met for each patient
risk_conditions <- (patients$Age > 50) + (patients$Hypertensive == "Yes")

patients$Risk_Level <- ifelse(risk_conditions == 2, "High Risk",
                              ifelse(risk_conditions == 1, "Moderate Risk", "Low Risk"))
print(patients)
##      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

Section C – Loops and Iteration

Problem 11: Daily Data Entry Reminder

for (day in 1:7) {
  cat("Please upload lab data for Day", day, "\n")
}
## Please upload lab data for Day 1 
## Please upload lab data for Day 2 
## Please upload lab data for Day 3 
## Please upload lab data for Day 4 
## Please upload lab data for Day 5 
## Please upload lab data for Day 6 
## Please upload lab data for Day 7

Problem 12: Sequential Year Tracker

for (year in 2015:2025) {
  cat("Processing year:", year, "\n")
}
## Processing year: 2015 
## Processing year: 2016 
## Processing year: 2017 
## Processing year: 2018 
## Processing year: 2019 
## Processing year: 2020 
## Processing year: 2021 
## Processing year: 2022 
## Processing year: 2023 
## Processing year: 2024 
## Processing year: 2025

Problem 13: Viral Load Tracking

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

for (vl in viral_loads) {
  if (vl < 1000) {
    status <- "Normal"
  } else if (vl >= 1000 & vl <= 9999) {
    status <- "Elevated"
  } else {
    status <- "Critical"
  }
  cat("Viral Load:", vl, "->", status, "\n")
}
## Viral Load: 100 -> Normal 
## Viral Load: 550 -> Normal 
## Viral Load: 1200 -> Elevated 
## Viral Load: 20000 -> Critical 
## Viral Load: 850 -> Normal

Section D – Medium to Hard

Problem 14: Automated Clinical Categorization

# 1. Create the data frame
hiv_data <- data.frame(
  Name = c("Rahim", "Sumaiya", "Babul", "Joya"),
  CD4_Count = c(120, 480, 230, 700),
  stringsAsFactors = FALSE
)

# 2. Add Immunity_Status column
hiv_data$Immunity_Status <- ifelse(hiv_data$CD4_Count < 200, "Severe Immunodeficiency",
                                   ifelse(hiv_data$CD4_Count >= 200 & hiv_data$CD4_Count <= 500, "Moderate", "Normal"))
print("Full HIV Dataset:")
## [1] "Full HIV Dataset:"
print(hiv_data)
##      Name CD4_Count         Immunity_Status
## 1   Rahim       120 Severe Immunodeficiency
## 2 Sumaiya       480                Moderate
## 3   Babul       230                Moderate
## 4    Joya       700                  Normal
# 3. Print only "Severe" cases
severe_cases <- subset(hiv_data, Immunity_Status == "Severe Immunodeficiency")
print("Severe Cases Only:")
## [1] "Severe Cases Only:"
print(severe_cases)
##    Name CD4_Count         Immunity_Status
## 1 Rahim       120 Severe Immunodeficiency

Problem 15: Integrating Loops and Conditionals for Clinical Scoring

scores <- c(10, 35, 50, 80, 95)
Severity_Label <- character(length(scores))

# 1. Use a for loop and if-else to assign severity
for (i in seq_along(scores)) {
  if (scores[i] < 30) {
    Severity_Label[i] <- "Mild"
  } else if (scores[i] >= 30 & scores[i] <= 70) {
    Severity_Label[i] <- "Moderate"
  } else {
    Severity_Label[i] <- "Severe"
  }
}

# 2 & 3. Store results and combine in a data frame
scoring_df <- data.frame(
  Infection_Score = scores,
  Severity_Label = Severity_Label,
  stringsAsFactors = FALSE
)
print(scoring_df)
##   Infection_Score Severity_Label
## 1              10           Mild
## 2              35       Moderate
## 3              50       Moderate
## 4              80         Severe
## 5              95         Severe

Problem 16: Combine All Skills – Hospital Ward Analysis

# 1. Create the data frame
ward_data <- 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),
  stringsAsFactors = FALSE
)

# 2 & 3. Write a loop to classify each patient and add "Condition" column
ward_data$Condition <- character(nrow(ward_data))

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

print("Hospital Ward Data with Conditions:")
## [1] "Hospital Ward Data with Conditions:"
print(ward_data)
##     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   At Risk
## 6 Nayeem  58 36.5   97    Stable
# 4. Print a summary table of how many patients fall into each category
print("Summary Table of Conditions:")
## [1] "Summary Table of Conditions:"
print(table(ward_data$Condition))
## 
##  At Risk Critical   Stable 
##        2        1        3