These exercises are about the conditions and loops sections of Introduction to R.
Exercise 1 - If Else
x<- 42
if (x<0){
print("It's a negative number!")
}
x <- -42
if (x<0){
print("It's a negative number!")
}## [1] "It's a negative number!"
## [1] "It's not a negative number!"
## [1] "It's a negative number!"
x <- -1
if (x<0){
print("It's a negative number!")
}else if (x==0){
print("It's zero")
}else{
print("It's a positive number!")
}## [1] "It's a negative number!"
x <- 0
if (x<0){
print("It's a negative number!")
}else if (x==0){
print("It's zero")
}else{
print("It's a positive number!")
}## [1] "It's zero"
x <- 1
if (x<0){
print("It's a negative number!")
}else if (x==0){
print("It's zero")
}else{
print("It's a positive number!")
}## [1] "It's a positive number!"
Hint: The modulus operator may be useful here i.e. x%%2 returns the remainder after the value of x is divided by 2.
## [1] "1 is odd"
## [1] "2 is even"
– Using an ifelse() expression, create a factor from a vector of 1 to 40 where all numbers less than 10 are “small”,10 to 30 are “mid”,31 to 40 are “big”
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
vectorResult <- ifelse(condExercise<10,"small",ifelse(condExercise < 31,"mid","big"))
temp <- factor(vectorResult,levels=c("small","mid","big"),order=T)
temp## [1] small small small small small small small small small mid mid mid
## [13] mid mid mid mid mid mid mid mid mid mid mid mid
## [25] mid mid mid mid mid mid big big big big big big
## [37] big big big big
## Levels: small < mid < big
– Calculate the factorial (factorial of 3 = 3 * 2 * 1) of 10 using a loop.
for(x in 1:10){
if(x == 1){
factorialAnswer <- 1
}else{
factorialAnswer <- factorialAnswer * x
}
}
factorialAnswer## [1] 3628800
– Adjusting your answer from before, what is the first number that has a factorial greater than 1000.
factorialAnswer <- 0
count <- 0
while(factorialAnswer <= 1000){
count <- count+1
if(count == 1){
factorialAnswer <- 1
}else{
factorialAnswer <- factorialAnswer * count
}
}
count## [1] 7
filesToRead <- dir("ExpressionResults/",pattern = "*\\.txt",full.names=T)
fileRead <- vector("list",length=length(filesToRead))
for(i in 1:length(filesToRead)){
fileRead[[i]] <- read.delim(filesToRead[i],header=F,sep="\t")
colnames(fileRead[[i]]) <- c("GeneNames",basename(filesToRead[i]))
}
mergedTable <- NULL
for(i in fileRead){
if(is.null(mergedTable)){
mergedTable <- i
}else{
mergedTable <- merge(mergedTable,i,by=1,all=T)
}
print(nrow(mergedTable))
}## [1] 5001
## [1] 5001
## [1] 5001
## [1] 5001
## [1] 5001
## [1] 5001
## [1] 5001
## [1] 5001
## [1] 5001
## [1] 5001
## [1] 5001
## GeneNames Annotation.txt <NA> ExpressionResults_Sample1.txt
## 1 Gene_1 Ens_1001 DNA_Binding 3.448466
## 2 Gene_10 Ens_10010 DNA_Binding 5.314180
## 3 Gene_100 Ens_100100 DNA_Binding 5.591612
## ExpressionResults_Sample10.txt ExpressionResults_Sample2.txt
## 1 7.665488 5.250063
## 2 7.813501 5.361170
## 3 5.186500 6.840497
## ExpressionResults_Sample3.txt ExpressionResults_Sample4.txt
## 1 5.968927 6.868251
## 2 5.305980 6.742855
## 3 5.197710 5.922931
## ExpressionResults_Sample5.txt ExpressionResults_Sample6.txt
## 1 5.367100 5.189686
## 2 5.957786 6.293098
## 3 6.813154 6.228178
## ExpressionResults_Sample7.txt ExpressionResults_Sample8.txt
## 1 3.882930 5.329258
## 2 7.361497 6.649428
## 3 5.831575 6.653152
## ExpressionResults_Sample9.txt
## 1 6.167451
## 2 6.213910
## 3 3.992555
– Add annotation from Annotation.txt. How do the pathway information for genes compare between expression table and annotation table.
Annotation <- read.table("ExpressionResults/Annotation.txt",sep="\t",h=T)
annotatedExpression <- merge(Annotation,mergedTable,by=1,all.x=F,all.y=T)
annotatedExpression[1:2,]## GeneName Ensembl Pathway Annotation.txt <NA>
## 1 Gene_1 Ens_1001 DNA_Binding Ens_1001 DNA_Binding
## 2 Gene_10 Ens_10010 DNA_Binding Ens_10010 DNA_Binding
## ExpressionResults_Sample1.txt ExpressionResults_Sample10.txt
## 1 3.448466 7.665488
## 2 5.314180 7.813501
## ExpressionResults_Sample2.txt ExpressionResults_Sample3.txt
## 1 5.250063 5.968927
## 2 5.361170 5.305980
## ExpressionResults_Sample4.txt ExpressionResults_Sample5.txt
## 1 6.868251 5.367100
## 2 6.742855 5.957786
## ExpressionResults_Sample6.txt ExpressionResults_Sample7.txt
## 1 5.189686 3.882930
## 2 6.293098 7.361497
## ExpressionResults_Sample8.txt ExpressionResults_Sample9.txt
## 1 5.329258 6.167451
## 2 6.649428 6.213910
## Length Class Mode
## 5001 character character
## Length Class Mode
## 5000 character character