knitr::opts_chunk$set(echo = TRUE)
Problem 1: Add Blood
Sugar Readings
add_glucose <- function(a, b) {
alpha <- a + b
return(alpha)
}
# Test with my made-up values
add_glucose(120, 130)
## [1] 250
Problem 2: Calculate
BMI
calculate_BMI <- function(weight, height) {
alpha <- weight / (height^2)
return(alpha)
}
# Test
calculate_BMI(72, 1.68)
## [1] 25.5102
Problem 3: Multiply
Experimental Readings
enzyme_product <- function(a, b, c) {
alpha <- a * b * c
return(alpha)
}
# Test absorbance
enzyme_product(0.96, 1.31, 1.26)
## [1] 1.584576
Problem 4: : Add
Default Value Parameter
assay_ratio <- function(treatment, control = 100) {
alpha <- treatment / control
return(alpha)
}
# Without specifying control control would be 100 as default
assay_ratio(80)
## [1] 0.8
# With specifying control
assay_ratio(80, 120)
## [1] 0.6666667
Problem 5: Welcome
Message for New Students
welcome_student <- function(name) {
alpha <- paste("Welcome to the R for Biostatistics course, ", name, "!")
return(alpha)
}
welcome_student("Mahmud")
## [1] "Welcome to the R for Biostatistics course, Mahmud !"
#Let's make it a loop what we have learnt in our class for all classmates
welcome_student2 <- function(name) {
alpha <- paste("Welcome to the R for Biostatistics course, ", name, "!")
return(alpha)
}
for(i in c("Gopal","Hamid","Mahmud","Prarthana","Nabila","Rimon","Abdullah")){
print(welcome_student(i))
}
## [1] "Welcome to the R for Biostatistics course, Gopal !"
## [1] "Welcome to the R for Biostatistics course, Hamid !"
## [1] "Welcome to the R for Biostatistics course, Mahmud !"
## [1] "Welcome to the R for Biostatistics course, Prarthana !"
## [1] "Welcome to the R for Biostatistics course, Nabila !"
## [1] "Welcome to the R for Biostatistics course, Rimon !"
## [1] "Welcome to the R for Biostatistics course, Abdullah !"
Problem 6: Summary
Statistics for Blood Pressure
bp_summary <- function(x) {
alpha <- list(
Mean = mean(x),
Median = median(x),
Standard_Deviation = sd(x),
Minimum = min(x),
Maximum = max(x)
)
return(alpha)
}
#Test
x <- c(120, 135, 140, 150, 125, 138, 145, 132, 128, 134)
bp_summary(x)
## $Mean
## [1] 134.7
##
## $Median
## [1] 134.5
##
## $Standard_Deviation
## [1] 9.080504
##
## $Minimum
## [1] 120
##
## $Maximum
## [1] 150
Problem 7: Calculate
Total Cholesterol
#We learned this in the class while writing our own mean function
total_cholesterol <- function(x) {
total <- 0
for(i in 1: length(x)){
total <- total + x[i]
}
return(total)
}
#Test
cholesterol <- c(180, 190, 200, 210, 195)
total_cholesterol(cholesterol)
## [1] 975
Problem 8: Compute the
Mean Using a Loop
my_mean <- function(x) {
total <- 0
for (i in 1:length(x)) {
total <- total + x[i]
}
mean_value <- total / length(x)
return(mean_value)
}
my_mean(cholesterol)
## [1] 195
Problem 9: Generate
Multiple Messages
#### We did it already in Assignment 4 Question #11
Problem 10: Annual
Record Tracker
#### We did it already in Assignment 4 Question #12
Problem 11: Import
Clinical Data
clinical_data <- read.csv("clinical_data.csv", header = TRUE)
print(clinical_data)
## Patient_ID Age BMI Glucose BP
## 1 P001 45 24.5 95 120
## 2 P002 50 30.1 130 140
## 3 P003 38 28.0 110 130
## 4 P004 60 33.4 160 155
## 5 P005 55 27.2 145 148
## 6 P006 42 25.9 102 118
## 7 P007 47 31.0 120 135
## 8 P008 63 29.8 170 160
Problem 12: Add a
Derived Column
clinical_data$Risk_Index <- (clinical_data$BMI * clinical_data$Glucose) / clinical_data$BP
print(clinical_data)
## Patient_ID Age BMI Glucose BP Risk_Index
## 1 P001 45 24.5 95 120 19.39583
## 2 P002 50 30.1 130 140 27.95000
## 3 P003 38 28.0 110 130 23.69231
## 4 P004 60 33.4 160 155 34.47742
## 5 P005 55 27.2 145 148 26.64865
## 6 P006 42 25.9 102 118 22.38814
## 7 P007 47 31.0 120 135 27.55556
## 8 P008 63 29.8 170 160 31.66250
Problem 13: Write
Processed Data to a File
?write.csv
## starting httpd help server ... done
write.csv(clinical_data, "cleaned_clinical_data2.csv", row.names = FALSE) # If I don't provide row.name or set is as TRUE, it counts the row name of any data as separate column, and make row names with 1, 2, 3 .....
Problem 14: Create a
Function for Gene Expression Summary
# Looks like I have to fix if else first, otherwise a bracket problem arise
gene_summary <- function(x) {
if (mean(x) > 500) {
category <- "High Expression"
} else {
category <- "Low Expression"
}
alpha <- list(
mean(x),
median(x),
category
)
return(alpha)
}
# Test data
TP53 <- c(450, 520, 600, 480, 550, 700, 490, 530, 610, 470)
gene_summary(TP53)
## [[1]]
## [1] 540
##
## [[2]]
## [1] 525
##
## [[3]]
## [1] "High Expression"