knitr::opts_chunk$set(echo = TRUE)
volunteer_names <- c('Hashem', 'Haris','Mostafa')
print(volunteer_names[1])
## [1] "Hashem"
patient_ids <- c(1:4)
print(patient_ids)
## [1] 1 2 3 4
student_names <- c('Maria', 'Shirmin', 'Alam')
print(class(student_names))
## [1] "character"
bp <- c(120, 132, 138)
print(bp[2:3])
## [1] 132 138
expression <- c(230, 120, 450)
names(expression) <- c("TP53", "BRCA1", "MYC")
print(expression["BRCA1"])
## BRCA1
## 120
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
print(class(patient_by_expression))
## [1] "matrix" "array"
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!
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
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
print("already added in problem 10")
## [1] "already added in problem 10"
print(gluc_level[1,3])
## [1] 120
#or
print(gluc_level["Asif","Treatment2"])
## [1] 120
new_gluc_matrix <- gluc_level[c(1,3),c(1,3)]
print(new_gluc_matrix)
## Baseline Treatment2
## Asif 90 120
## Afridi 80 110
harun_gluc_level <- gluc_level[2, c(1,2)]
print(harun_gluc_level)
## Baseline Treatment1
## 100 180
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
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)
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
print(df[c(1,2),c("Patient","Age")])
## Patient Age
## 1 Tamim 35
## 2 Imrul 39
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
rownames(df) <- df$Patient
df$Patient <- NULL
print(df)
## Age Mutations Married
## Tamim 35 4 TRUE
## Imrul 39 1 TRUE
## Fiz 37 0 TRUE
df[1, "Mutations"] <- 5
print(df)
## Age Mutations Married
## Tamim 35 5 TRUE
## Imrul 39 1 TRUE
## Fiz 37 0 TRUE
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