2.1.6 Programming Exercises

In this lesson, you will solve, by yourselves, the following four questions. They are purely about R, to complement the theoretical exercise from two weeks ago. All the information necessary to solve it is below, in the “Basic R” section.

Open RStudio, write the solutions in the text editor panel (top left), and show me when each one is ready.

1 Exercises

  1. Sum all the numbers from 1 to 1000. You can do it using the syntax 1:1000, which generates a vector with all numbers between 1 and 1000, and the function sum. Do the same with the function mean, which calculates the mean.

  2. Write a function called sayHi that takes a parameter name, and returns the sentence Hi name!. For this, you will use the function paste, which can be used as follows:

    > paste("A","B")
    [1] "A B"
    > paste("A","B",sep="")
    [1] "AB"
  3. Create a vector with the name of four of your classmates, using the c(...) construction. Use a for loop to go through these names, an print the say hi sentence for each one of them, using the print function, and the sayHi function that was defined just above.

  4. Write a function that takes a vector numbers as argument, and for each one tells if the number is big (>10) or small, using the if/else construction presented in the Conditionals part.

2 Basic R

This section serves as a reference on how to use the most useful constructs in R. Make sure to come back to this page if you forget the syntax of the operations.

2.1 Variables

R can act like a basic calculator that you type commands in to. You can also use it like a more advanced scientific calculator and create variables that store information. In R, we assign values to variables using an arrow-looking function <- the assignment operator. For example, we can assign the value 2*3 to the variable x using the command:

> x <- 2*3

To view the contents of any variable, just type its name, press enter, and the contents of that R object will be displayed:

> x
[1] 6

What kind of information can you store in a variable? Anything! But the simplest kinds of data are strings, numbers and booleans:

> s <- "Some String"
> n <- 154.4
> b <- TRUE

2.2 Vectors

To create a vector, we can use the c() (combine) function. For example, to create a vector called myvector that has elements with values 8, 6, 9, 10, and 5, we type:

myvector <- c(8, 6, 9, 10, 5) # note: commas between each number!

To see the contents of the variable myvector, we can just type its name and press enter:

> myvector
[1]  8  6  9 10  5

The [1] is the index of the first element in the vector. We can extract any element of the vector by typing the vector name with the index of that element given in square brackets [...].

For example, to get the value of the 4th element in the vector myvector, we type:

> myvector[4]
[1] 10

Vectors can also contain letters, such as those designating nucleic acids

my.seq <- c("A","T","C","G")

They can also contain multi-letter strings:

my.oligos <- c("ATCGC","TTTCGC","CCCGCG","GGGCGC")

2.3 Tables

If we made a vector variable “nucleotides” containing the of a DNA molecule, we can use the table() function to produce a table variable that contains the number of bases with each possible nucleotides:

bases <- c("A", "T", "A", "A", "T", "C", "G", "C", "G")

Now make the table

> table(bases)
bases
A C G T 
3 2 2 2

2.4 Arguments

Functions in R usually require arguments, which are input variables (i.e.. objects) that are passed to them, which they then carry out some operation on. For example, the log10() function is passed a number, and it then calculates the log to the base 10 of that number:

> log10(100)
[1] 2

There’s a more generic function, log(), where we pass it not only a number to take the log of, but also the specific base of the logarithm. To take the log base 10 with the log() function we do this

> log(100, base = 10)
[1] 2

We can also take logs with other bases, such as 2:

> log(100, base = 2)
[1] 6.643856

We can perform computations with R using objects such as scalars and vectors. For example, to calculate the average of the values in the vector myvector (i.e.. the average of 8, 6, 9, 10 and 5), we can use the mean() function:

> mean(myvector) # note: no " "
[1] 7.6

We have been using built-in R functions such as mean(), length(), print(), plot(), etc.

2.5 Writing your own functions

We can also create our own functions in R to do calculations that you want to carry out very often on different input data sets. For example, we can create a function to calculate the value of 20 plus square of some input number, in the code panel:

myfunction <- function(x) {
    y <- x * x
    return(20 + y)
}

This function will calculate the square of a number x, and then add 20 to that value. The return() statement returns the calculated value. Once you have typed in this function, the function is then available for use. For example, we can use the function for different input numbers (e.g.. 10, 25):

> myfunction(10)
[1] 120

2.6 Conditionals

It is often useful to execute different operations depending on a condition. Let’s say we want to say if a number if big (larger than 10) or small (otherwise):

x <- 5
if (x > 10) {
  print("Big!")
} else {
  print("Small!")
}

By changing the value of x, you can see either the first or the second piece of code being executed.

2.7 Loops

Finally, sometimes we want to execute an operation many times. The way to do it is using a for loop. For example, to print the numbers from 1 to 10 we can do:

for (x in 1:10) {  
  print(x)  
}

Or, to print every element of an array:

dice <- c(1, 2, 3, 4, 5, 6)

for (x in dice) {  
  print(x)  
}