You are building a simple registry of three patients enrolled in a diabetes study: “Rahim”, “Karim”, and “Sultana”.
Create a character vector with their names and print the second patient.
# character vector
patients <- c("Rahim", "Karim", "Sultana")
# second patient
patients[2]
## [1] "Karim"
Systolic blood pressure (SBP) values were recorded for four hypertensive patients: 145, 160, 138, 152.
Store these in a numeric vector and extract the 3rd and 4th patients’ SBP.
sbp <- c(145, 160, 138, 152)
sbp[c(3, 4)]
## [1] 138 152
Expression counts for the gene TP53 were measured in four samples: 500, 800, 450, 600.
Create a vector and calculate the mean expression.
tp53_counts <- c(500, 800, 450, 600)
mean(tp53_counts)
## [1] 587.5
Create a data frame from the dataset (Name, Age, BMI).
name <- c("Ali", "Sonia", "Kabir")
age <- c(35, 29, 41)
bmi <- c(24.5, 28.2, 31.0)
cohort_df <- data.frame(Name = name, Age = age, BMI = bmi)
cohort_df
## Name Age BMI
## 1 Ali 35 24.5
## 2 Sonia 29 28.2
## 3 Kabir 41 31.0
For clinical interpretation, classify BMI into: “Normal” if < 25; “Overweight” if 25–29.9; “Obese” if ≥ 30
Add a new column “BMI_Class” to the data frame.
cohort_df$BMI_Class <- ifelse(cohort_df$BMI < 25, "Normal",
ifelse(cohort_df$BMI < 30, "Overweight",
"Obese"))
cohort_df
## Name Age BMI BMI_Class
## 1 Ali 35 24.5 Normal
## 2 Sonia 29 28.2 Overweight
## 3 Kabir 41 31.0 Obese
Researchers want only participants older than 30 years.
Use conditional indexing to filter the data frame.
cohort_over30 <- cohort_df[cohort_df$Age > 30, ]
cohort_over30
## Name Age BMI BMI_Class
## 1 Ali 35 24.5 Normal
## 3 Kabir 41 31.0 Obese
The team wants to see patients classified as “Obese”.
Filter the data frame to select only those rows.
cohort_obese <- cohort_df[cohort_df$BMI_Class == "Obese", ]
cohort_obese
## Name Age BMI BMI_Class
## 3 Kabir 41 31 Obese
You measured expression of three genes (BRCA1, EGFR, MYC) across
three patients.
Gene P1 P2 P3
BRCA1 120 150 130
EGFR 300 350 400
MYC 800 900 950
Create this as a matrix with proper row and column names.
# Create a gene expression matrix
gene_expression <- matrix(
c(120, 150, 130,
300, 350, 400,
800, 900, 950),
nrow = 3,
byrow = TRUE)
# Assign row names (genes)
rownames(gene_expression) <- c("BRCA1", "EGFR", "MYC")
# Assign column names (patients)
colnames(gene_expression) <- c("P1", "P2", "P3")
# Display the matrix
gene_expression
## P1 P2 P3
## BRCA1 120 150 130
## EGFR 300 350 400
## MYC 800 900 950
You want MYC’s expression for Patient 2.
Extract this value from your matrix.
gene_expression["MYC", "P2"]
## [1] 900
You want to store different analysis objects together: 1. A vector of patient ages: 35, 29, 41 2. The gene expression matrix from Problem 8 3. The nutrition data frame from Problem 4
Create a list with these three objects.
patient_age <- c(25, 29, 41)
multi_list <- list(patientAge= patient_age,
GeneExpression= gene_expression,
Nutritional= cohort_df)
multi_list
## $patientAge
## [1] 25 29 41
##
## $GeneExpression
## P1 P2 P3
## BRCA1 120 150 130
## EGFR 300 350 400
## MYC 800 900 950
##
## $Nutritional
## Name Age BMI BMI_Class
## 1 Ali 35 24.5 Normal
## 2 Sonia 29 28.2 Overweight
## 3 Kabir 41 31.0 Obese
#P roblem 11: Extract from a List
You want to analyze EGFR expression across all patients.
Extract the second row of the matrix from the list.
multi_list$GeneExpression[2, ]
## P1 P2 P3
## 300 350 400
Fasting blood glucose (FBG) was measured for a patient and stored as x. • If FBG < 100 → “Normal” • If FBG 100–125 → “Prediabetes” • If FBG ≥ 126 → “Diabetes”
Write an if-else statement to classify the patient based on x.
# Assign fasting blood glucose value
x <- 115
# Classify blood glucose level
if (x < 100) {print("Normal") } else if (x >= 100 & x <= 125) {print("Prediabetes")} else {print("Diabetes")}
## [1] "Prediabetes"
A cardiovascular study defines “high risk” as patients who are both older
than 40 years AND have BMI ≥ 30.
Name Age BMI
Tania 42 31.5
Mahir 37 28.0
Jui 45 29.5
Imran 50 32.0
Use conditional indexing to extract patients classified as “high risk.”
# Create patient data frame
cv_patients <- data.frame(
Name = c("Tania", "Mahir", "Jui", "Imran"),
Age = c(42, 37, 45, 50),
BMI = c(31.5, 28.0, 29.5, 32.0))
# Display the data frame
cv_patients
## Name Age BMI
## 1 Tania 42 31.5
## 2 Mahir 37 28.0
## 3 Jui 45 29.5
## 4 Imran 50 32.0
# Select patients older than 40 years AND BMI >= 30
high_risk <- cv_patients[
cv_patients$Age > 40 & cv_patients$BMI >= 30, ]
# Display high-risk patients
high_risk
## Name Age BMI
## 1 Tania 42 31.5
## 4 Imran 50 32.0
In an oncology project, you need to store:
• A patient demographic data frame (Name, Age, Sex).
• A matrix of tumor marker expression values (CA125, CEA, PSA across 3 patients).
• A vector of survival times (months).
Create a list containing all three objects.
Extract:
o The CEA expression for the 2nd patient.
o The survival time for the 3rd patient.
# Patient demographic information
patient_demo <- data.frame(
Name = c("Tania", "Mahir", "Jui"),
Age = c(42, 37, 45),
Sex = c("Female", "Male", "Female"))
# Display demographic data
patient_demo
## Name Age Sex
## 1 Tania 42 Female
## 2 Mahir 37 Male
## 3 Jui 45 Female
# Tumor marker expression values
tumor_expression <- matrix(
c(120, 150, 130,
300, 350, 400,
800, 900, 950),
nrow = 3, byrow = TRUE)
# Assign row and column names
rownames(tumor_expression) <- c("CA125", "CEA", "PSA")
colnames(tumor_expression) <- c("P1", "P2", "P3")
# Display matrix
tumor_expression
## P1 P2 P3
## CA125 120 150 130
## CEA 300 350 400
## PSA 800 900 950
# Survival time in months
survival_time <- c(24, 36, 18)
# Combine all three objects into a list
oncology_data <- list(
Demographics = patient_demo,
Tumor_Expression = tumor_expression,
Survival_Time = survival_time)
# Display the list
oncology_data
## $Demographics
## Name Age Sex
## 1 Tania 42 Female
## 2 Mahir 37 Male
## 3 Jui 45 Female
##
## $Tumor_Expression
## P1 P2 P3
## CA125 120 150 130
## CEA 300 350 400
## PSA 800 900 950
##
## $Survival_Time
## [1] 24 36 18
# Extract the CEA expression for the second patient
oncology_data$Tumor_Expression["CEA", "P2"]
## [1] 350
# Extract the survival time for the third patient
oncology_data$Survival_Time[3]
## [1] 18
Hemoglobin (Hb) values were measured for 6 patients: 11.2, 13.5, 9.8, 14.1, 12.0, 8.7. WHO guidelines: • Hb < 12 → “Anemia” • Hb ≥ 12 → “Normal”
Store the Hb values in a vector. Use ifelse() to create a classification vector (“Anemia” or “Normal”). Add this classification as a new column in a data frame of patients.
# Store hemoglobin values
Hb <- c(11.2, 13.5, 9.8, 14.1, 12.0, 8.7)
# Display Hb values
Hb
## [1] 11.2 13.5 9.8 14.1 12.0 8.7
# Use ifelse() to classify anemia status
Anemia_Status <- ifelse(Hb < 12, "Anemia", "Normal")
# Display classification
Anemia_Status
## [1] "Anemia" "Normal" "Anemia" "Normal" "Normal" "Anemia"
# Create patient data frame
patient_data_An <- data.frame(
Patient_ID = c("P1", "P2", "P3", "P4", "P5", "P6"),
Hb = Hb,
Anemic_patient = Anemia_Status)
# Display patient data
patient_data_An
## Patient_ID Hb Anemic_patient
## 1 P1 11.2 Anemia
## 2 P2 13.5 Normal
## 3 P3 9.8 Anemia
## 4 P4 14.1 Normal
## 5 P5 12.0 Normal
## 6 P6 8.7 Anemia