# Programming in R

You *probably* won't need this information for your assignments

On the preceding pages we have tried to introduce the basics of the R language - but have managed to avoid anything you might need to actually write your own program: things like if statements, loops, and writing functions.

Relevant help pages can be found with `help("Control")` and `help("Function")`.

## For loops

In R a `while` takes this form, where *variable* is the name of your iteration variable, and *sequence* is a vector or list of values:

`for (`*variable* `in` *sequence*`)` *expression*

The *expression* can be a single R command - or several lines of commands wrapped in curly brackets:

for (variableinsequence) {expressionexpressionexpression}

Here is a quick trivial example, printing the square root of the integers one to ten:

> for (x in c(1:10)) print(sqrt(x)) [1] 1 [1] 1.414214 [1] 1.732051 [1] 2 [1] 2.236068 [1] 2.449490 [1] 2.645751 [1] 2.828427 [1] 3 [1] 3.162278

## While loops

In R a `while` takes this form, where *condition* evaluates to a boolean (True/False) and must be wrapped in ordinary brackets:

`while (`*condition*`)` *expression*

As with a `for` loop, *expression* can be a single R command - or several lines of commands wrapped in curly brackets:

while (condition) {expressionexpressionexpression}

We'll start by using a "while loop" to print out the first few Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, ... where each number is the sum of the previous two numbers. Create a new R script file, and copy this code into it:

a <- 0 b <- 1 print(a) while (b < 50) { print(b) temp <- a + b a <- b b <- temp }

If you go to the script's "Edit" menu and pick "Run all" you should get something like this in the R command console:

> a <- 0 > b <- 1 > print(a) [1] 0 > while (b < 50) { + print(b) + temp <- a + b + a <- b + b <- temp + } [1] 1 [1] 1 [1] 2 [1] 3 [1] 5 [1] 8 [1] 13 [1] 21 [1] 34

The code works fine, but both the output and the R commands are both shown in the R command window - its a bit messy.

This next version builds up the answer gradually using a vector, which it prints at the end:

x <- c(0,1) while (length(x) < 10) { position <- length(x) new <- x[position] + x[position-1] x <- c(x,new) } print(x)

To understand how this manages to append the `new` value to the end of the vector `x`, try this at the command prompt:

> x <- c(1,2,3,4) > c(x,5) [1] 1 2 3 4 5

## Writing Functions

This following script uses the `function()` command to create a function (based on the code above) which is then stored as an object with the name `Fibonacci`:

Fibonacci <- function(n) { x <- c(0,1) while (length(x) < n) { position <- length(x) new <- x[position] + x[position-1] x <- c(x,new) } return(x) }

Once you run this code, there will be a new function available which we can now test:

> Fibonacci(10) [1] 0 1 1 2 3 5 8 13 21 34 > Fibonacci(3) [1] 0 1 1 > Fibonacci(2) [1] 0 1 > Fibonacci(1) [1] 0 1

That seems to work nicely - except in the case `n == 1` where the function is returning the first *two* Fibonacci numbers! This gives us an excuse to introduce the `if` statement.

## The If statement

In order to fix our function we can do this:

Fibonacci <- function(n) { if (n==1) return(0) x <- c(0,1) while (length(x) < n) { position <- length(x) new <- x[position] + x[position-1] x <- c(x,new) } return(x) }

In the above example we are using the simplest possible if statement:

if (condition)expression

The `if` statement can also be used like this:

if (condition)expressionelseexpression

And, much like the `while` and `for` loops the *expression* can be multiline with curly brackets:

Fibonacci <- function(n) { if (n==1) { x <- 0 } else { x <- c(0,1) while (length(x) < n) { position <- length(x) new <- x[position] + x[position-1] x <- c(x,new) } } return(x) }

Do you like this version better that the previous one?