knitr::opts_chunk$set(echo = TRUE)

1 Section D - Factors

1.1 Problem 24: Academic Titles

#we have learnt the exact same code in the class
academic_title <- c("lecturer","assistant professor","associate prof","Professor")
academic_title <- factor(academic_title, levels = c("lecturer","assistant professor","associate prof","Professor"))
print(academic_title)
## [1] lecturer            assistant professor associate prof     
## [4] Professor          
## Levels: lecturer assistant professor associate prof Professor

1.2 Problem 25: Lab Storage Temperature

temperature <- c("cold", "medium", "hot", "cold", "hot")
temperature <- factor(temperature,levels = c("medium", "cold", "hot"))
print(temperature)
## [1] cold   medium hot    cold   hot   
## Levels: medium cold hot

1.3 Problem 1: Identifying Patients with Abnormal Glucose

glucose_level <- matrix(c(95, 160, 120,110, 190, 170,85, 150, 130,98, 200, 180,102, 175, 150), nrow = 5, byrow = TRUE)
rownames(glucose_level) <- c("P1", "P2", "P3", "P4", "P5")
colnames(glucose_level) <- c("Fasting", "1-hour", "2-hour")

print(glucose_level)
##    Fasting 1-hour 2-hour
## P1      95    160    120
## P2     110    190    170
## P3      85    150    130
## P4      98    200    180
## P5     102    175    150
## very important lesson to learn. I was trying to define ifelse with $ sign, for example. glucose_level(glucose_level$Fasting >=100, ) like that, but it never worked. Then learnt that $ cannot be used for matrix, it works on data.frame. So, either I have to convert matrix into data.frame or I have to use [] for matrix position selection.

abnormal <- ifelse(glucose_level[, "Fasting"] >= 100 & glucose_level[, "1-hour"] > 180 & glucose_level[, "2-hour"] >= 140, "Abnormal" ,"Normal")

# It didn't work!!!

1.4 Problem 2: Differential Gene Expression Analysis

gene <-  c("TP53", "BRCA1", "MYC", "EGFR", "ACTB")
healthy <- c(250, 120, 500, 300, 1000)
tumor <- c(800, 130, 2000, 900, 1000)
gene_df <- data.frame(gene, healthy, tumor)

# Make a fold change column
gene_df$fc <- gene_df$tumor / gene_df$healthy
print(gene_df)
##    gene healthy tumor       fc
## 1  TP53     250   800 3.200000
## 2 BRCA1     120   130 1.083333
## 3   MYC     500  2000 4.000000
## 4  EGFR     300   900 3.000000
## 5  ACTB    1000  1000 1.000000
# write a ifelse to determine status
gene_df$Status <- ifelse(gene_df$fc >= 2,"Upregulated", ifelse(gene_df$fc <= 0.5, "Downregualted", "No Change"))
print (gene_df)
##    gene healthy tumor       fc      Status
## 1  TP53     250   800 3.200000 Upregulated
## 2 BRCA1     120   130 1.083333   No Change
## 3   MYC     500  2000 4.000000 Upregulated
## 4  EGFR     300   900 3.000000 Upregulated
## 5  ACTB    1000  1000 1.000000   No Change
# Extract only upregulated genes
upregulated_genes <- gene_df[gene_df$Status == "Upregulated", ]
print(upregulated_genes)
##   gene healthy tumor  fc      Status
## 1 TP53     250   800 3.2 Upregulated
## 3  MYC     500  2000 4.0 Upregulated
## 4 EGFR     300   900 3.0 Upregulated

1.5 Problem 3: Clinical Trial Stratification

# Create the data frame
id = c("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8")
age = c(45, 50, 39, 60, 55, 42, 48, 52)
baseline_bp = c(160, 150, 145, 170, 160, 135, 155, 162)
post_bp = c(140, 135, 142, 155, 150, 130, 138, 145)
trial_df <- data.frame(id, age, baseline_bp, post_bp)
print(trial_df)
##   id age baseline_bp post_bp
## 1 P1  45         160     140
## 2 P2  50         150     135
## 3 P3  39         145     142
## 4 P4  60         170     155
## 5 P5  55         160     150
## 6 P6  42         135     130
## 7 P7  48         155     138
## 8 P8  52         162     145
# Calculate BP reduction
trial_df$bp_drop <- trial_df$baseline_bp - trial_df$post_bp
print(trial_df)
##   id age baseline_bp post_bp bp_drop
## 1 P1  45         160     140      20
## 2 P2  50         150     135      15
## 3 P3  39         145     142       3
## 4 P4  60         170     155      15
## 5 P5  55         160     150      10
## 6 P6  42         135     130       5
## 7 P7  48         155     138      17
## 8 P8  52         162     145      17
# Write a ifelse to determine response
trial_df$response <- ifelse(trial_df$bp_drop >= 15, "responder", "non responder")

# Display the complete data frame
print(trial_df)
##   id age baseline_bp post_bp bp_drop      response
## 1 P1  45         160     140      20     responder
## 2 P2  50         150     135      15     responder
## 3 P3  39         145     142       3 non responder
## 4 P4  60         170     155      15     responder
## 5 P5  55         160     150      10 non responder
## 6 P6  42         135     130       5 non responder
## 7 P7  48         155     138      17     responder
## 8 P8  52         162     145      17     responder
# Extract respondents older than 50
responders_over50 <- trial_df[trial_df$response == "responder" & trial_df$age >50, ] 

print (responders_over50)
##   id age baseline_bp post_bp bp_drop  response
## 4 P4  60         170     155      15 responder
## 8 P8  52         162     145      17 responder

1.6 Problem 4: Cancer Staging with Factors

# Create dataframe
patient <- c("P1", "P2", "P3", "P4", "P5")
stage <- c("Stage II", "Stage IV", "Stage I","Stage III", "Stage II")

cancer_df <- data.frame(patient, stage)
print(cancer_df)
##   patient     stage
## 1      P1  Stage II
## 2      P2  Stage IV
## 3      P3   Stage I
## 4      P4 Stage III
## 5      P5  Stage II
# Convert into factor
cancer_df$stage <- factor(cancer_df$stage, levels = c("Stage I", "Stage II", "Stage III", "Stage IV", order=TRUE))

print(cancer_df)
##   patient     stage
## 1      P1  Stage II
## 2      P2  Stage IV
## 3      P3   Stage I
## 4      P4 Stage III
## 5      P5  Stage II
# Counting requires table function
table(cancer_df$stage)
## 
##   Stage I  Stage II Stage III  Stage IV      TRUE 
##         1         2         1         1         0
# Extract Stage III or Stage IV patients
advanced_cancer <- cancer_df[cancer_df$stage == "Stage III" | cancer_df$stage == "Stage IV",] # & doesn't work here, Google asked to use | for or

# Google also suggested to make it simple without the use of piping
advanced_cancer <- cancer_df[cancer_df$stage %in% c("Stage III", "Stage IV"),]

print (advanced_cancer)
##   patient     stage
## 2      P2  Stage IV
## 4      P4 Stage III

1.7 Problem 5 Epidemiology – Identifying High-Risk Cities

city <- c("Dhaka", "Chittagong", "Rajshahi", "Sylhet", "Khulna", "Barisal")
population_in_mil <- c(21, 9, 3, 2, 5, 1.5)
cases_in_k <- c(350, 150, 40, 70, 60, 15)
covid_df <- data.frame(city, population_in_mil, cases_in_k)

print(covid_df)
##         city population_in_mil cases_in_k
## 1      Dhaka              21.0        350
## 2 Chittagong               9.0        150
## 3   Rajshahi               3.0         40
## 4     Sylhet               2.0         70
## 5     Khulna               5.0         60
## 6    Barisal               1.5         15
#The easiest way to calculate cases per 100,000 is:Cases per 100,000 = Cases in thousands / Population in millions × 1000

covid_df$cases_in_100k <- (covid_df$cases_in_k / covid_df$population_in_mil) * 1000

print(covid_df)
##         city population_in_mil cases_in_k cases_in_100k
## 1      Dhaka              21.0        350      16666.67
## 2 Chittagong               9.0        150      16666.67
## 3   Rajshahi               3.0         40      13333.33
## 4     Sylhet               2.0         70      35000.00
## 5     Khulna               5.0         60      12000.00
## 6    Barisal               1.5         15      10000.00
# write a ifelse for determing risk-level
covid_df$risk_level <- ifelse(covid_df$cases_in_100k >=200, "High", ifelse(covid_df$cases_in_100k >=200, "Moderate", "Low"))

print(covid_df)
##         city population_in_mil cases_in_k cases_in_100k risk_level
## 1      Dhaka              21.0        350      16666.67       High
## 2 Chittagong               9.0        150      16666.67       High
## 3   Rajshahi               3.0         40      13333.33       High
## 4     Sylhet               2.0         70      35000.00       High
## 5     Khulna               5.0         60      12000.00       High
## 6    Barisal               1.5         15      10000.00       High
# Extract Which city has high risk
covid_df_highrisk <- covid_df[covid_df$risk_level == "High", ] 

print (covid_df_highrisk)
##         city population_in_mil cases_in_k cases_in_100k risk_level
## 1      Dhaka              21.0        350      16666.67       High
## 2 Chittagong               9.0        150      16666.67       High
## 3   Rajshahi               3.0         40      13333.33       High
## 4     Sylhet               2.0         70      35000.00       High
## 5     Khulna               5.0         60      12000.00       High
## 6    Barisal               1.5         15      10000.00       High