Introduction to Biological Data Analysis


TP 1: Introduction to coding in R

Prof. Patrick Meyer

BioSys Lab - Université de Liège

Assignations

  1. If the variables a, b, d have as value, respectively 2, 6 et 1, what are their values after the following lines of codes (this execise should be performed mentally)?

    a) > a <- b   d) > d <- a
      > d <- b     > a <- b
            > b <- d
    b) > a <- a + 1   e) > b <- -a
      > b <- d + 1     > b <- 2 * b
      > d <- 2 * d     > a <- b
    c) > a <- b   f) > a <- a * a
      > d <- a     > a <- a * a

  2. Write a few lines of R code allowing to swap the values of $a$ and $b$.

Functions

  1. What is the purpose of the following function (mental exercise)?
    > fction1 <- function(eu)
    {
        fb <- 40.3399*eu
        fb
    }
    

    What will be displayed on screen if you type the following instructions:

        > fction1(100)
        > fction1(200000)
        > fction1(fction1(1))
    

  2. What is the purpose of the following function (mental exercise)?
    > fction2 <- function(a,b)
    {
        a <- a + b
        a + b
    }
    

    What will be displayed on screen if you type the following instructions:

        > b <- 8
        > a <- 13
        > fction2(b,a)
        > a
    

  3. Write a function $bmi()$ which given two parameters $weight$ and $height$, computes the corresponding body mass index $bmi=\frac{weight}{height^2}$.

  4. Write a function that converts Farhenheit into Celsius, knowing that you just have to substract $32$ degrees before multiplying by $\frac{5}{9}$.

  5. The Euclidean distance between two points $(x_1, y_1)$ and $(x_2, y_2)$ is given by the formula:

    \begin{displaymath}s = \sqrt{(x_1-x_2)^2 + (y_1-y_2)^2} \end{displaymath}

    Write a function that receives four values corresponding to the xy coordinates of two points and computes the Euclidean distance between them (you can use the function $sqrt$).

Vectors

  1. What would be displayed on screen if you type the following lines of code (this execise should be performed mentally):
      > weight <- c(60, 72, 57, 90, 95, 72)
      > height <- c(1.75, 1.80, 1.65, 1.90, 1.74, 1.91)
      > bmi(weight,height)
    

  2. What would be displayed on screen if you type the following lines of code (this execise should be performed mentally):
      > a <- c(TRUE,FALSE,FALSE,TRUE)
      > b <- c(FALSE,FALSE,TRUE,TRUE)
      > b|a
      > a&!b
    

  3. Given the following assignations:
     > e <- c(1,2,3,5,8,13,21,34,55)
     > f <- c(4,6,7,9)
     > g <- rep("hello",10)
     > h <- 50:60
    
    What would be displayed on screen if you type the following lines of code (this execise should be performed mentally):
     > e[5]
     > e[6:9]
     > h[f[1:3]]
     > e[f-2]
     > g[e[f-3]]
     > c(e,f)
     > f[c(TRUE,FALSE,TRUE,FALSE)]
     > f > 7
     > f[f > 6]
     > f[-2]
     > h[-c(4,6)]
    

  4. Given two vectors $POS$ and $DAT$, write the code allowing to display the elements of $DAT$ in the order given by $POS$.

    Example

    DAT: 9 6 8 2
    POS: 2 4 3 1

    returns $6 \;\; 2 \;\; 8 \;\; 9$.

  5. Write a code that performs the scalar product of two vectors ($u,v$) given that the formula for vectors of length $n$ is :

    \begin{displaymath}\sum_{i=1}^n u_i \cdot v_i \end{displaymath}

    You may use the function $sum()$.