R Tutorial 5: Basic Programming Continued

For/While Loops

So, what’s a loop? A loop allows you to execute a series of action over and over again within a few lines of code. For the concept of loops, we will be using the Fibonacci Sequence as an example.

A Fibonacci sequence is where a number is a sum of the two numbers preceding it, and the first two numbers are both 1.

Going by the brute-force method would require us to do the below, which is a pain to type:

#creating a vector of length 10
a <- vector(length=10)
#then we set the first two base cases
a[1] <- 1
a[2] <- 1
a[3] <- a[1]+a[2]
a[4] <- a[2]+a[3]
a[5] <- a[3]+a[4]
a[6] <- a[4]+a[5]
a[7] <- a[5]+a[6]
a[8] <- a[6]+a[7]
a[9] <- a[7]+a[8]
a[10] <- a[8]+a[9]
## [1] 1 1 2 3 5 8 13 21 34 55

So here is where loops come into play. Firstly, let’s look at a For Loop.

b <- vector(length=10)
#for i, an arbitrary variable starting from 3 to length of a (which is 10)
#so, R will perform the statements within the for loop with i values changing from 3,4,5,6,7,8,9,10
for (i in 3:length(b)){
## [1] 1 1 2 3 5 8 13 21 34 55

Then we can look at a while loop.

c <- vector(length=10)
#while the value i is not 11, continue to perform the while loop
  #let i increase by 1
 ## [1] 1 1 2 3 5 8 13 21 34 55

You may ask, so what’s the difference between a “For” and a “While” loop. There is not much difference, as long you achieve your goal of doing something repetitively. Usually for loops are used for repeating commands that you know how many times it should be going, such as the example above. While loop is usually used for things you know you want it to continue until a certain condition is fulfilled.

When you are browsing materials for R during self-study, you might see articles that tell you to “AVOID LOOPS IN R”, they are just advocating more efficient of doing repetitive coding that you can learn along the way.

Pre-defined functions

Take for instance, if I were to ask you to calculate the mean of a numerical vector c(1,2,3), you probably will do the below:

C <- c(1,2,3)
sumofC <- 0
for (i in 1:3){
  sumofC <- C[i] + sumofC
meanofC <- sumofC/length(C)
 ## [1] 2

Versus using a predefined function in R called mean()

C<- c(1,2,3)
 ## [1] 2

Well, the choice is clear. Always do look out for inbuilt functions in R that you can use, such as quantile. Non-inbuilt functions can be accessed once you installed the specific package, which is code done by some kind soul to save you from hardcoding some calculations from scratch.

User-defined Functions

When you code, there are times when you realise you will be using the same code over and over again. You could consider copy-pasting the code multiple times, and risk screwing up some variable handling somewhere, or you can type your own functions, and give it a name!

Writing a function always involve the same structure:
*function_name<- function(argument1,argument2)

Take the example of Fibonacci sequence code that we previously coded:

fibonnaci_sequence <- function(num_terms){
  #initialise a vector of a certain length
  for (i in 3:num_terms){
    a[i]<- a[i-1]+a[i-2]
## [1] 1 1 2 3
## [1] 1 1 2 3 5 8 13 21 34 55

User-defined functions are good to know, but since we are not computer science students, let’s just hope whatever function we need is already been coded by some kind soul

Next: Tutorial 6 Data Manipulation


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s