Task: Create a character vector with their names and print the second patient.
## [1] "Karim"
Task: Store these in a numeric vector and extract the 3rd and 4th patients’ SBP.
## [1] 138 152
Task: Create a vector and calculate the mean expression.
## [1] 587.5
Task: Create a data frame from the dataset (Name, Age, BMI).
# Create vectors
name <- c("Ali", "Sonia", "Kabir")
age <- c(35, 29, 41)
bmi <- c(24.5, 28.2, 31.0)
# Create data frame
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
Task: Add a new column “BMI_Class” based on WHO guidelines.
# Add BMI_Class column using nested ifelse
cohort_df$BMI_Class <- ifelse(cohort_df$BMI < 25, "Normal",
ifelse(cohort_df$BMI >= 25 & cohort_df$BMI <= 29.9, "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
Task: Use conditional indexing to filter patients older than 30 years.
## Name Age BMI BMI_Class
## 1 Ali 35 24.5 Normal
## 3 Kabir 41 31.0 Obese
Task: Filter the data frame to select only those classified as “Obese”.
## Name Age BMI BMI_Class
## 3 Kabir 41 31 Obese
Task: Create a matrix with proper row and column names for BRCA1, EGFR, and MYC.
# Create matrix data
gene_data <- c(120, 150, 130, # BRCA1
300, 350, 400, # EGFR
800, 900, 950) # MYC
# Build matrix
gene_matrix <- matrix(gene_data, nrow = 3, ncol = 3, byrow = TRUE)
# Assign row and column names
rownames(gene_matrix) <- c("BRCA1", "EGFR", "MYC")
colnames(gene_matrix) <- c("P1", "P2", "P3")
gene_matrix## P1 P2 P3
## BRCA1 120 150 130
## EGFR 300 350 400
## MYC 800 900 950
Task: Extract MYC’s expression for Patient 2.
## [1] 900
Task: Create a list with ages, the gene expression matrix, and the nutrition data frame.
ages_vec <- c(35, 29, 41)
multi_list <- list(
PatientAges = ages_vec,
GeneExpression = gene_matrix,
NutritionData = cohort_df
)
# Print the list structure
multi_list## $PatientAges
## [1] 35 29 41
##
## $GeneExpression
## P1 P2 P3
## BRCA1 120 150 130
## EGFR 300 350 400
## MYC 800 900 950
##
## $NutritionData
## Name Age BMI BMI_Class
## 1 Ali 35 24.5 Normal
## 2 Sonia 29 28.2 Overweight
## 3 Kabir 41 31.0 Obese
Task: Extract the second row of the matrix from the list (EGFR expression).
## P1 P2 P3
## 300 350 400
Task: Write an if-else statement to classify FBG
stored as x.
# Assign a sample value to x for demonstration
x <- 115
if (x < 100) {
status <- "Normal"
} else if (x >= 100 & x <= 125) {
status <- "Prediabetes"
} else {
status <- "Diabetes"
}
cat("Glucose Status for x =", x, "is:", status, "\n")## Glucose Status for x = 115 is: Prediabetes
Task: Create the data frame and extract “high risk” patients (Age > 40 AND BMI >= 30).
# 1. Create data frame
cardio_name <- c("Tania", "Mahir", "Jui", "Imran")
cardio_age <- c(42, 37, 45, 50)
cardio_bmi <- c(31.5, 28.0, 29.5, 32.0)
cardio_df <- data.frame(Name = cardio_name, Age = cardio_age, BMI = cardio_bmi)
cat("Original Cardio Data Frame:\n")## Original Cardio Data Frame:
## Name Age BMI
## 1 Tania 42 31.5
## 2 Mahir 37 28.0
## 3 Jui 45 29.5
## 4 Imran 50 32.0
# 2. Extract high-risk patients
high_risk <- cardio_df[cardio_df$Age > 40 & cardio_df$BMI >= 30, ]
cat("\nHigh-Risk Patients:\n")##
## High-Risk Patients:
## Name Age BMI
## 1 Tania 42 31.5
## 4 Imran 50 32.0
Task: Create the list and extract specific data points.
# 1. Create Objects
# Demographic data frame
demo_df <- data.frame(Name = c("Pt_A", "Pt_B", "Pt_C"),
Age = c(55, 62, 48),
Sex = c("F", "M", "M"))
# Matrix of tumor markers (CA125, CEA, PSA)
tumor_vals <- c(35.2, 12.5, 4.1, # CA125
5.0, 8.2, 2.1, # CEA
1.2, 4.5, 0.8) # PSA
tumor_matrix <- matrix(tumor_vals, nrow = 3, byrow = TRUE)
rownames(tumor_matrix) <- c("CA125", "CEA", "PSA")
colnames(tumor_matrix) <- c("P1", "P2", "P3")
# Vector of survival times
survival_vec <- c(24, 18, 36) # months
# Create List
onco_list <- list(Demographics = demo_df,
TumorMarkers = tumor_matrix,
SurvivalTimes = survival_vec)
# 2. Extractions
cat("CEA expression for the 2nd patient:\n")## CEA expression for the 2nd patient:
## [1] 8.2
##
## Survival time for the 3rd patient:
## [1] 36
Task: Classify Hb values and add to a data frame.
# 1. Store Hb values
hb_vals <- c(11.2, 13.5, 9.8, 14.1, 12.0, 8.7)
# 2. Use ifelse() to classify
anemia_class <- ifelse(hb_vals < 12, "Anemia", "Normal")
# 3. Add to a data frame
patient_ids <- paste0("Patient_", 1:6)
final_anemia_df <- data.frame(ID = patient_ids,
Hemoglobin = hb_vals,
Status = anemia_class)
final_anemia_df## ID Hemoglobin Status
## 1 Patient_1 11.2 Anemia
## 2 Patient_2 13.5 Normal
## 3 Patient_3 9.8 Anemia
## 4 Patient_4 14.1 Normal
## 5 Patient_5 12.0 Normal
## 6 Patient_6 8.7 Anemia