Aim

  • Learn Programing in R

Programming in R

Till this moment we were calling each built-in function and executing mostly in single lines. Now Let’s do some reals programming, which will include:

  • Conditional Statements
  • Looping
  • Functions

Conditional Statements

Sometimes we needs to take decisions based on condition(s).

In programming languages for doing this is called a conditional statement, and looks like this:

## [1] "not greater"
## [1] "done"

The second line of this code uses an if statement to tell R that we want to make a choice. If the following test is true, the body of the if (i.e., the lines in the curly braces underneath it) are executed. If the test is false, the body of the else is executed instead. Only one or the other is ever executed:

img: if-else

img: if-else

In the example above, the test num > 100 returns the value FALSE, which is why the code inside the if block was skipped and the code inside the else statement was run instead.

## [1] FALSE

And as you likely guessed, the opposite of FALSE is TRUE.

## [1] TRUE

Conditional statements don’t have to include an else. If there isn’t one, R simply does nothing if the test is false:

## [1] "num is less than 100"

Exercise-1

Write a if-else condition statement to find if a number is even or odd.

Solution

## [1] "Number is even"

Exercise-2

Write a if-else condition statement to find if a number is positive.

Solution

## [1] "Positive number"

Note

The test for equality uses two equal signs, ==. Other tests include greater than or equal to (>=), less than or equal to (<=), and not equal to (!=). We can also combine tests. Two ampersands, &&, symbolize “and”. Two vertical bars, ||, symbolize “or”. && is only true if both parts are true.

## [1] "at least one part is not true"

while || is true if either part is true:

## [1] "at least one part is true"

In this case, “either” means “either or both”, not “either one or the other but not both”.

Looping

In computer programming, a loop is a sequence of instructions that is continually repeated until a certain condition is reached.

img: looping

img: looping

  • R has two basic types of loop
    • a for loop: run some code on every value in a vector
    • a while loop: run some code while some condition is true (hardly ever used!)

Functions

If we only had one data set to analyze, it would probably be faster to load the file into a spreadsheet and use that to plot some simple statistics. But we have twelve files to check, and may have more in the future. In this lesson, we’ll learn how to write a function so that we can repeat several operations with a single command.

Defining a Function

Let’s start by defining a function fahrenheit_to_kelvin that converts temperatures from Fahrenheit to Kelvin:

We define fahrenheit_to_kelvin by assigning it to the output of function. The list of argument names are contained within parentheses. Next, the body of the function–the statements that are executed when it runs–is contained within curly braces ({}). The statements in the body are indented by two spaces, which makes the code easier to read but does not affect how the code operates.

When we call the function, the values we pass to it are assigned to those variables so that we can use them inside the function. Inside the function, we use a return statement to send a result back to whoever asked for it.

Automatic Returns

In R, it is not necessary to include the return statement. R automatically returns whichever variable is on the last line of the body of the function. While in the learning phase, we will explicitly define the return statement.

Let’s try running our function. Calling our own function is no different from calling any other function:

## [1] 273.15
## [1] 373.15

We’ve successfully called the function that we defined, and we have access to the value that we returned.

Composing Functions

Now that we’ve seen how to turn Fahrenheit into Kelvin, it’s easy to turn Kelvin into Celsius:

## [1] -273.15

What about converting Fahrenheit to Celsius? We could write out the formula, but we don’t need to. Instead, we can compose the two functions we have already created:

## [1] 0

This is our first taste of how larger programs are built: we define basic operations, then combine them in ever-larger chunks to get the effect we want. Real-life functions will usually be larger than the ones shown here–typically half a dozen to a few dozen lines–but they shouldn’t ever be much longer than that, or the next person who reads it won’t be able to understand what’s going on.

Nesting Functions

This example showed the output of fahrenheit_to_kelvin assigned to temp_K, which is then passed to kelvin_to_celsius to get the final result. It is also possible to perform this calculation in one line of code, by “nesting” one function inside another, like so

## [1] 0

Exercise

In the last lesson, we learned to combine elements into a vector using the c function, e.g. x <- c("A", "B", "C") creates a vector x with three elements. Furthermore, we can extend that vector again using c, e.g. y <- c(x, "D") creates a vector y with four elements. Write a function called highlight that takes two vectors as arguments, called content and wrapper, and returns a new vector that has the wrapper vector at the beginning and end of the content:

Solution

## [1] "***"       "Write"     "programs"  "for"       "people"    "not"      
## [7] "computers" "***"

Exercise

If the variable v refers to a vector, then v[1] is the vector’s first element and v[length(v)] is its last (the function length returns the number of elements in a vector). Write a function called edges that returns a vector made up of just the first and last elements of its input:

Solution

## [1] "Don't"  "others"

Key Points

  • Divide a program into small chunks of functions for easy interpretability.
 

Created and Maintained by Sangram Keshari Sahu
Rmarkdown Template used from Rmdplates package
Licensed under CC-BY 4.0
Source Code At GitHub