knitr::opts_chunk$set(echo = TRUE)

1 Problem 1: Creating a Dataset of Patients

#install.packages("dplyr")
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Create dataset
patients_data <- data.frame(
  ID = 1:10,
  Name = c("Ali", "Sara", "Rahim", "Joya", "Tom", "Anika", "Mehedi", "Rita", "Karim", "Elina"),
  Age = c(45, 34, 50, 29, 42, 38, 41, 33, 47, 31),
  Gender = c("M", "F", "M", "F", "M", "F", "M", "F", "M", "F"),
  BP_Sys = c(120, 110, 145, 130, 150, 138, 160, 125, 148, 135),
  BP_Dia = c(80, 70, 90, 85, 95, 88, 100, 75, 92, 82),
  Cholesterol = c(200, 180, 250, 195, 270, 210, 300, 190, 280, 205),
  Diagnosis = c("Hypertension", "Normal", "Hypertension", "Normal", "Hypertension",
                "Borderline", "Hypertension", "Normal", "Hypertension", "Borderline")
)

View(patients_data)

# We also want to save this data as csv
write.csv(patients_data,"I:/OxfordBioDiscovery2025/2026/Single cell RNAseq/patients_data.csv")

2 Problem 2: Select Key Clinical Columns (Name, Age, Cholesterol, and Diagnosis)

patients_data %>%
  select(Name, Age,Cholesterol,Diagnosis)
##      Name Age Cholesterol    Diagnosis
## 1     Ali  45         200 Hypertension
## 2    Sara  34         180       Normal
## 3   Rahim  50         250 Hypertension
## 4    Joya  29         195       Normal
## 5     Tom  42         270 Hypertension
## 6   Anika  38         210   Borderline
## 7  Mehedi  41         300 Hypertension
## 8    Rita  33         190       Normal
## 9   Karim  47         280 Hypertension
## 10  Elina  31         205   Borderline

3 Problem 3: Filter Patients by Age and Gender (Female and over 35)

patients_data_female <- patients_data %>%
  filter(Age > 35, Gender == "F")

View(patients_data_female)

4 Problem 4: Identify Hypertensive Males

patients_data_male_hyper <- patients_data %>%
  filter( Gender == "M", Diagnosis == "Hypertension")

View(patients_data_male_hyper)

5 Problem 5: Select Borderline Female Patients

patients_data_female_brdline <- patients_data %>%
  filter( Gender == "F", Diagnosis == "Borderline")

View(patients_data_female_brdline)

6 Problem 6: Arrange by Cholesterol Level (descending)

patients_data_chol_arrang <- patients_data %>%
  arrange(desc(Cholesterol))

View(patients_data_chol_arrang)

7 Problem 7: Create a Combined Blood Pressure Score (BP_Sum = BP_Sys + BP_Di)

patients_data_bp_sum <- patients_data %>%
  mutate(BP_sum = BP_Sys + BP_Dia)

View(patients_data_bp_sum)

8 Problem 8: Calculate Average Age and Cholesterol

patients_data_summarise <- patients_data %>%
  summarise(mean_age = mean(Age),
            mean_chol = mean(Cholesterol))

View(patients_data_summarise)

9 Problem 9: Compare Cholesterol by Diagnosis Group

patients_data_group <- patients_data %>%
  group_by(Diagnosis) %>%
  summarise(mean_age = mean(Age),
            mean_cholesterol = mean(Cholesterol),
            patient_count = n())

View(patients_data_group)

10 Problem 10: Identify Highest Risk Group

patients_data_hrg <- patients_data %>%
  group_by(Diagnosis) %>%
  summarise(mean_age = mean(Age),
            mean_cholesterol = mean(Cholesterol),
            patient_count = n()) %>%
  arrange(desc(mean_cholesterol))

View(patients_data_hrg)

11 Problem 11: Analyze Female Subgroup

patients_data_afg <- patients_data %>%
  filter(Gender == "F") %>%
  group_by(Diagnosis) %>%
  summarise(mean_age = mean(Age),
            mean_cholesterol = mean(Cholesterol),
            patient_count = n()) %>%
  arrange(desc(mean_cholesterol))

View(patients_data_afg)

12 Problem 12: Remove Derived Variables

patients_data_clean <- patients_data %>%
  select(ID, Name, Age, Gender, BP_Sys, BP_Dia, Cholesterol, Diagnosis)

View(patients_data_clean)

13 Problem 13: Convert Blood Pressure Data to Long Format

patients_data_clean <- patients_data %>%
  select(ID, Name, Age, Gender, BP_Sys, BP_Dia, Cholesterol, Diagnosis)

View(patients_data_clean)

14 Problem 14: Convert Blood Pressure Data to Long Format

library(tidyr)

bp_long <- patients_data |>
  pivot_longer(
    cols = c(BP_Sys, BP_Dia), # I want only two columns, that is why selected with C()
    names_to = "BP_Type",
    values_to = "Value"
  )

View(bp_long)

15 Problem 15: Compare Two Study Cohorts (Medium–Hard)

cohort_A <- patients_data

# Follow-up cohort
cohort_B <- c(2, 3, 7, 10, 11, 12)

#1 Common patients
common_patients <- intersect(cohort_A$ID, cohort_B)

View(common_patients)

#2 Patients present only in Cohort A
unique_A <- setdiff(cohort_A$ID, cohort_B)

View(unique_A)

#3 Print Results
print(common_patients)
## [1]  2  3  7 10
print(unique_A)
## [1] 1 4 5 6 8 9