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!"
x <- 0

if (x<0){
  print("It's a negative number!")
}else{
  print("It's not a negative number!")
}
## [1] "It's not a negative number!"
x <- -1

if (x<0){
  print("It's a negative number!")
}else{
  print("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.

x <- 1

if (x%%2==0){
  paste(x,"is even")
}else{
  paste(x,"is odd")
}
## [1] "1 is odd"
x <- 2

if (x%%2==0){
  paste(x,"is even")
}else{
  paste(x,"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”

condExercise <- 1:40
condExercise
##  [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
setwd("~/Downloads/Intro_To_R_1Day-master/r_course")
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
mergedTable[1:3,] 
##   GeneNames Annotation.txt          NA ExpressionResults_Sample1.txt
## 1  GeneName        Ensembl     Pathway                            NA
## 2    Gene_1       Ens_1001 DNA_Binding                      3.448466
## 3   Gene_10      Ens_10010 DNA_Binding                      5.314180
##   ExpressionResults_Sample10.txt ExpressionResults_Sample2.txt
## 1                             NA                            NA
## 2                       7.665488                      5.250063
## 3                       7.813501                      5.361170
##   ExpressionResults_Sample3.txt ExpressionResults_Sample4.txt
## 1                            NA                            NA
## 2                      5.968927                      6.868251
## 3                      5.305980                      6.742855
##   ExpressionResults_Sample5.txt ExpressionResults_Sample6.txt
## 1                            NA                            NA
## 2                      5.367100                      5.189686
## 3                      5.957786                      6.293098
##   ExpressionResults_Sample7.txt ExpressionResults_Sample8.txt
## 1                            NA                            NA
## 2                      3.882930                      5.329258
## 3                      7.361497                      6.649428
##   ExpressionResults_Sample9.txt
## 1                            NA
## 2                      6.167451
## 3                      6.213910

– 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 GeneName     <NA>        <NA>        Ensembl     Pathway
## 2   Gene_1 Ens_1001 DNA_Binding       Ens_1001 DNA_Binding
##   ExpressionResults_Sample1.txt ExpressionResults_Sample10.txt
## 1                            NA                             NA
## 2                      3.448466                       7.665488
##   ExpressionResults_Sample2.txt ExpressionResults_Sample3.txt
## 1                            NA                            NA
## 2                      5.250063                      5.968927
##   ExpressionResults_Sample4.txt ExpressionResults_Sample5.txt
## 1                            NA                            NA
## 2                      6.868251                        5.3671
##   ExpressionResults_Sample6.txt ExpressionResults_Sample7.txt
## 1                            NA                            NA
## 2                      5.189686                       3.88293
##   ExpressionResults_Sample8.txt ExpressionResults_Sample9.txt
## 1                            NA                            NA
## 2                      5.329258                      6.167451
summary(annotatedExpression$Pathway)
##    Length     Class      Mode 
##      5001 character character
summary(Annotation$Pathway)
##    Length     Class      Mode 
##      5000 character character