These exercises are about the functions sections of Introduction to R.

Exercise 1 - Functions

– Create a function which takes one number and returns the square of that number

squareOfNumber <- function(number){
  squareNumber <- number^2
  return(squareNumber)
}

squareOfNumber(3)
## [1] 9

– Create a function which takes two numbers and returns the mean

meanOfNumbers <- function(number,number2){
  meanOfNumbersRes <- mean(c(number,number2))
  return(meanOfNumbersRes)
}

meanOfNumbers(1,3)
## [1] 2
meanOfVectors <- function(vector,vector2){
  bigvector <- c(vector,vector2)
  message(paste("The total length of my vectors is", length(bigvector)))
  meanOfVectorsRes <- mean(bigvector )
  return(meanOfVectorsRes)
}

meanOfVectors(c(1,3,4),c(3,6,1,7,9))
## The total length of my vectors is 8
## [1] 4.25
dfAndVecReturn <- function(number,number2){
  input <- c(number,number2)
  df <- data.frame(mean=mean(c(number,number2)),
                   sum=number+number2,
                   multiple=number*number2
                   )
  return(list(input,df))
}

dfAndVecReturn(1,3)
## [[1]]
## [1] 1 3
## 
## [[2]]
##   mean sum multiple
## 1    2   4        3

– Create a function which takes one argument and finds the smallest number whose factorial is greater than that argument.

findSmallestFactorial <- function(x){
    factorialAnswer <- 0
    count <- 0
    while(factorialAnswer <= x){
      count <- count+1
      if(count == 1){
        factorialAnswer <- 1
      }else{
        factorialAnswer <- factorialAnswer * count 
      }
    }
    return(count)
}

findSmallestFactorial(3000)
## [1] 7
findSmallestFactorial(10^100)
## [1] 70

– Add a if and else statement in your function to only calculate factorial code if argument is a numeric.

findSmallestFactorial <- function(x){
  if(!is.numeric(x)){
    message("Please provide a numeric argument!")
  }else{
    factorialAnswer <- 0
    count <- 0
    while(factorialAnswer <= x){
      count <- count+1
      if(count == 1){
        factorialAnswer <- 1
      }else{
        factorialAnswer <- factorialAnswer * count 
      }
    }
    return(count)
  }
}

findSmallestFactorial(3000)
## [1] 7
findSmallestFactorial("Hello")
## Please provide a numeric argument!

Exercise 2 - Scripts

Lets try to put as much together that we have learnt thus far. This will be a multistep challenge. Break it down and use pseudocode to help. Start by working the code interactively, then turn it into a script.

  1. Read in the “data/GeneExpression.txt” dataset.
  2. Use a apply to calculate the Z score for each gene (per row). The zscore is (gene_expression - mean)/standard deviation. You should use a function to do this calculation.
  3. Find which gene has the highest absolute max Zscore. This is a very rough proxy for the variability of that gene.
  4. Print out the gene name with the highest value
  5. Turn this into a script and run the script
  6. Think about what modifications you would need to make in order to accept a different data set as input.
geneExpression <- read.table("data/GeneExpression.txt",h=T,sep="\t",row.names=1)
geneExpression <-as.matrix(geneExpression)
zscores <- function(x){
    my_mean <- mean(x)
    my_sd <- sd(x)
    my_z <- (x-my_mean)/my_sd
    return(my_z)}

my_zs <- apply(geneExpression,2, zscores)

inds <- which(my_zs == max(my_zs), arr.ind = T)       
rownames(geneExpression)[inds[,1]]
## [1] "Gene_h"