knitr::opts_chunk$set(echo = TRUE)
Problem 1: Patient
Registry
patient_names <- c("Rahim", "Karim", "Sultana")
print(patient_names[2])
## [1] "Karim"
Problem 2: Blood
Pressure Data
SBP <- c(145, 160, 138, 152)
print(SBP [c(3,4)]) #always use c(,) format when try to retrieve two data points from a vector.
## [1] 138 152
Problem 3: Gene
Expression Counts
TP53_expression <- c(500, 800, 450, 600)
?mean
## starting httpd help server ... done
#Function appeared as mean(x, trim = 0, na.rm = FALSE, ...), but I think only mean should work here
print (mean(TP53_expression))
## [1] 587.5
Problem 4: Small Cohort
Data Frame
name <-c("Ali", "Sonia", "Kabir")
age <- c(35, 29, 41)
bmi <- c(24.5, 28.2, 31.0)
nutrition_df <-data.frame(name,age, bmi)
colnames(nutrition_df) <- c("Name", "Age", "BMI")
print(nutrition_df)
## Name Age BMI
## 1 Ali 35 24.5
## 2 Sonia 29 28.2
## 3 Kabir 41 31.0
Problem 5: Add BMI
Classification
# I'm gonna use ifelse function
nutrition_df$BMI_Class <- ifelse(nutrition_df$BMI <25, "Normal", ifelse(nutrition_df$BMI >= 25 & nutrition_df$BMI <=29.9, "Overweight", "Obese")) # for final argument we don't need to set anything, it indicates if anything doesn't lie within first two condition, it will be "Obese"
print(nutrition_df)
## Name Age BMI BMI_Class
## 1 Ali 35 24.5 Normal
## 2 Sonia 29 28.2 Overweight
## 3 Kabir 41 31.0 Obese
Problem 6: Subset
Patients by Age
older_than_30 <- nutrition_df[nutrition_df$Age > 30, ]
print(older_than_30)
## Name Age BMI BMI_Class
## 1 Ali 35 24.5 Normal
## 3 Kabir 41 31.0 Obese
Problem 7: Subset
Patients by BMI
Obese <- nutrition_df[nutrition_df$BMI_Class == "Obese", ]
print(Obese)
## Name Age BMI BMI_Class
## 3 Kabir 41 31 Obese
Problem 8: Create a
Gene Expression Matrix ypes
gene_expression <- matrix( c(120, 150, 130,300, 350, 400,800, 900, 950),nrow = 3,byrow = TRUE)
rownames(gene_expression) <- c("BRCA1", "EGFR", "MYC")
colnames(gene_expression) <- c("P1", "P2", "P3")
print(gene_expression)
## P1 P2 P3
## BRCA1 120 150 130
## EGFR 300 350 400
## MYC 800 900 950
Problem 10: Create a
Multi-Object List
ages <- c(35, 29, 41)
multi_object_list <- list(ages, gene_expression, nutrition_df)
print(multi_object_list)
## [[1]]
## [1] 35 29 41
##
## [[2]]
## P1 P2 P3
## BRCA1 120 150 130
## EGFR 300 350 400
## MYC 800 900 950
##
## [[3]]
## Name Age BMI BMI_Class
## 1 Ali 35 24.5 Normal
## 2 Sonia 29 28.2 Overweight
## 3 Kabir 41 31.0 Obese
Problem 12:
Conditional Statement for Glucose Check
x <-120
# I learned this in Oxford Biodiscovery Arafat Bhai's Microbial Genomics class on python. Asked ChatGPT what I can use in R to mimic python's x = float(input("Enter fasting blood glucose: ")). Got the following:
#x <- as.numeric(readline("Enter fasting blood glucose: "))
if (x < 100) {
print("Normal")
} else if (x >= 100 && x <= 125) {
print("Prediabetes")
} else {
print("Diabetes")
}
## [1] "Prediabetes"
Problem 13: Identify
High-Risk Patients
#creating dataframe
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)
)
patients
## Name Age BMI
## 1 Tania 42 31.5
## 2 Mahir 37 28.0
## 3 Jui 45 29.5
## 4 Imran 50 32.0
#finding high-risk patients
high_risk <- patients[patients$Age > 40 & patients$BMI >= 30, ]
print(high_risk)
## Name Age BMI
## 1 Tania 42 31.5
## 4 Imran 50 32.0
Problem 14: Integrate
Different Bio-medical Data in a List
#Can do. But I was wondering whether I can input any data. Also, it is time consuming
Problem 15: Automated
Classification of Anemia Status
Hb <- c(11.2, 13.5, 9.8, 14.1, 12.0, 8.7)
# I can use a simple ifelse function to define anemia just like problem #5
status <- ifelse(Hb < 12, "Anemia", "Normal")
print(status)
## [1] "Anemia" "Normal" "Anemia" "Normal" "Normal" "Anemia"
#adding to a patient dataframe
patients_hb <- data.frame(Patient = c("P1", "P2", "P3", "P4", "P5", "P6"), Hb, status)
print(patients_hb)
## Patient Hb status
## 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