R Programming

Control structures

Allow you to control the flow of execution of the program

  • if, else -> testing a condition

  • for -> execute a loop a fixed number of times

  • while -> execute a function while a condition is true

  • repeat -> execute an infinite loop

  • break -> break the execution of a loop

  • next -> skip an iteration of a loop

  • return -> exit a function

If, else

In R you can assign the result of a if,else structure to a variable

y <- if(x>3) {10} else {0}

for loop

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

for(i in 1:5) OR for(i in seq_along(x)

seq_along() takes a vector and creates an integer sequence equal to the length of the vector

while loop

Initiate a variable count before the loop and increment it at every iteration

Writting Functions

  • R always returns the result of the last expression of the function

  • Functions are created by the function() and stored as R objects

  • Functions have named arguments that may have default values

  • The formal arguments are those included in the function()

  • Formals() takes a function as an input and returns a list of its arguments

  • Arguments can be matched by name, position or partially

  • Lazy evaluation -> the arguments are only evaluated if they are needed

  • The “...” argument represents a variable number of arguments that are passed by a different function

  • The “...” argument is also necessary when you don’t know in advance the number of arguments to be passed to the function

  • Arguments that come after the “...” must be explicitly matched

Scoping Rules

How does R recognizes and gives values to different symbols, even though a previous version of this symbol has been used before?

When R tries to bind a value to a symbol, it searches through a series of environments to find the appropriate value. When you are working on the command line and need to retrieve the value of an R object, the order is roughly this:

  1. Search the global environment for a symbol name matching the one requested

  2. Search the namespaces of each packages currently loaded on the search list (this list can be found using search()

  • Every time you load a new package it is allocated in the second position

Scoping rules: (determines how a value is associated with a free variable in a function

  • R uses lexical scoping or static scoping. An alternative is dynamic scoping

  • Related to the scoping rules is how R uses the search list to bind a value to a symbol

  • Lexical scoping in R means that the value of free variables are searched for in the environment in which the function is defined

What is an environment?

  • A collection of (symbol, value) pairs, i.e. x is a symbol and 3.14 may be its value.

  • Every environment has a parent environment; it is possible for a single environment to have multiple “children”

  • The only environment without a parent is the empty environment

  • A function + an environment = a closure or function closure

What happens when you are ina function and finds a free variable?

  1. If the value is not found in the environment in which the function was defined, then the search continues in the parent environment

  2. The search continues down the sequence of parent environments until it reaches the top-level environment (usually the global environment or the namespace of a package

  3. After the top-level environment, the search continues down the search list until it hits the empty environment.

  4. If after all that the value is not found an error is thrown

In R the environment in which you create a function can be another function (nested functions):

make.power <- function(n){

pow <- function(x){

x^n

}

pow

}

AND NOW:

cube <- make.power(3)

cube (2)

8

You can use the ls() in a given environment() of a function

Lexical vs. Dynamic Scoping:

  • With lexical scoping the value of a free variable is looked in the environment in which the function is defined

  • With dynamic scoping the value is looked up in the environment from which the function was called (calling environment

Consequences of lexical scoping

  1. In R, all objects must be stored in the memory

  2. All functions must carry a pointer to their respective defining environments

  3. In S-PLUS, free variables are always looked up in the global workspace, so everything can be stored on the disk because the “defining environment” of all functions is the same

Coding standards for R

  1. Always use text files / text editor

  2. Indent your code

  3. Limit the width of your code ( 80 columns)

  4. Limit the length of your functions -> split different functionalities into different functions and helps with debugging

Dates and Times

R has developed a special representation of dates and times

  • Dates are represented by the date class

  • Times are represented by the POSIXct or the POSIXlt class

  • Dates are stored internally as the number of days since 1970-01-01

  • Tomes are stored internally as the number of seconds since 1970-01-01

as.Date() -> change an argument class to date class object