knitr::opts_chunk$set(echo = TRUE)

1 Section A - Vectors

1.1 Problem 1: Recording Volunteer Names

volunteer_names <- c('Hashem', 'Haris','Mostafa')
print(volunteer_names[1])
## [1] "Hashem"

1.2 Problem 2: Assigning Patient IDs

patient_ids <- c(1:4)
print(patient_ids)
## [1] 1 2 3 4

1.3 Problem 3: Student Participants in a Workshop

student_names <- c('Maria',  'Shirmin', 'Alam')
print(class(student_names))
## [1] "character"

1.4 Problem 4: Extracting Test Results

bp <- c(120, 132, 138)
print(bp[2:3])
## [1] 132 138

1.5 Problem 5: Labeling Gene Expression

expression <- c(230, 120, 450)
names(expression) <- c("TP53", "BRCA1", "MYC")
print(expression["BRCA1"])
## BRCA1 
##   120

2 Section B - Matrix

2.1 Problem 6: Creating an Expression Matrix

patient_by_expression <- matrix(data = c(1:12), nrow = 4, byrow = TRUE)
rownames(patient_by_expression) <- c('patient 1', 'patient 2', 'patient 3', 'patient 4')
print(patient_by_expression)
##           [,1] [,2] [,3]
## patient 1    1    2    3
## patient 2    4    5    6
## patient 3    7    8    9
## patient 4   10   11   12

2.2 Problem 7: Checking Data Structure

print(class(patient_by_expression))
## [1] "matrix" "array"

2.3 Problem 8: Mixing Data Types

mixing_data <- matrix(data = c("hello","hi","bye", 1,2,3), nrow =2)
print(mixing_data)
##      [,1]    [,2]  [,3]
## [1,] "hello" "bye" "2" 
## [2,] "hi"    "1"   "3"
#It converted everything into character!

2.4 Problem 9: Combining Patient Results into a Matrix

gluc_level <- matrix(data=c(90,160,120,100,180,150,80,145,110), nrow=3, ncol=3, byrow=TRUE)
rownames(gluc_level) <- c("Asif","Harun","Afridi")
colnames(gluc_level) <- c("fasting","1-hour","2-hour")
print(gluc_level)
##        fasting 1-hour 2-hour
## Asif        90    160    120
## Harun      100    180    150
## Afridi      80    145    110

2.5 Problem 10: Adding Column Names for Time Points

colnames(gluc_level) <- c(c("Baseline","Treatment1","Treatment2"))
print(gluc_level)
##        Baseline Treatment1 Treatment2
## Asif         90        160        120
## Harun       100        180        150
## Afridi       80        145        110

2.6 Problem 11: Adding Row Names for Patients

print("already added in problem 10")
## [1] "already added in problem 10"

2.7 Problem 12: Extracting a Specific Value

print(gluc_level[1,3])
## [1] 120
#or
print(gluc_level["Asif","Treatment2"])
## [1] 120

2.8 Problem 13: Comparing Two Patients

new_gluc_matrix <- gluc_level[c(1,3),c(1,3)]
print(new_gluc_matrix)
##        Baseline Treatment2
## Asif         90        120
## Afridi       80        110

2.9 Problem 14: Extracting Row Subsets

harun_gluc_level <- gluc_level[2, c(1,2)]
print(harun_gluc_level)
##   Baseline Treatment1 
##        100        180

3 Section C – Data Frames

3.1 Problem 16: Building a Patient Data Frame

name <- c("Tamim", "Imrul", "Fiz")
age <- c(35, 39, 37)
num_mut_detect <- c(4, 1, 0)
marital_status <- c(TRUE, TRUE, TRUE)

df <- data.frame(name, age, num_mut_detect, marital_status)
df
##    name age num_mut_detect marital_status
## 1 Tamim  35              4           TRUE
## 2 Imrul  39              1           TRUE
## 3   Fiz  37              0           TRUE

3.2 Problem 17: Exploring Data Frame Structure

str(df)
## 'data.frame':    3 obs. of  4 variables:
##  $ name          : chr  "Tamim" "Imrul" "Fiz"
##  $ age           : num  35 39 37
##  $ num_mut_detect: num  4 1 0
##  $ marital_status: logi  TRUE TRUE TRUE
View(df)

3.3 Problem 18: Renaming Columns

colnames(df) <- c("Patient",  "Age", "Mutations", "Married")
print(df)
##   Patient Age Mutations Married
## 1   Tamim  35         4    TRUE
## 2   Imrul  39         1    TRUE
## 3     Fiz  37         0    TRUE

3.4 Problem 19: Extracting a Subset of Data

print(df[c(1,2),c("Patient","Age")])
##   Patient Age
## 1   Tamim  35
## 2   Imrul  39

3.5 Problem 20: Extracting a Single Column

Background: A geneticist wants only the “Mutations” column to calculate mutation frequency. Task: Extract it using both df[,3] and df$Mutations.

print(df[,3])
## [1] 4 1 0
#or
print(df$Mutations)
## [1] 4 1 0

3.6 Problem 21: Adding Row Names

rownames(df) <- df$Patient
df$Patient <- NULL
print(df)
##       Age Mutations Married
## Tamim  35         4    TRUE
## Imrul  39         1    TRUE
## Fiz    37         0    TRUE

3.7 Problem 22: Updating a Value

df[1, "Mutations"] <- 5
print(df)
##       Age Mutations Married
## Tamim  35         5    TRUE
## Imrul  39         1    TRUE
## Fiz    37         0    TRUE

3.8 Problem 23: Sorting Patients by Age

df_age_ascend <- df[order(df$Age), ]
print(df_age_ascend)
##       Age Mutations Married
## Tamim  35         5    TRUE
## Fiz    37         0    TRUE
## Imrul  39         1    TRUE