Course 2 - R Programming - Week 2 - Notes

Greg Foletta

2019-09-26

Control Structures

Allow you to control the flow of a program.

If-Else

Standard if-else:

## [1] "False"

You can also assign from the construct:

## [1] 10

For

Standard for:

## [1] 1
## [1] 2
## [1] 3

Can use seq_along() to get integer vector:

## [1] "1 A"
## [1] "2 B"
## [1] "3 C"
## [1] "4 D"
## [1] "5 E"
## [1] "6 F"

While Loops

Standard while loop:

## [1] 4
## [1] 3
## [1] 2
## [1] 1

Repeat, Next, Break

Repeat is an infinite loop:

## [1] 1
## [1] 2
## [1] 3
## [1] 4

Functions

Functions are first class - treated like everything else.

Functons can be nested, so you can define a function inside another function.

Function Arguments

Formal arguments are the arguements included in the function definition. formals() function returns a list of format arguments.

## $x
## 
## 
## $...

Function arguments can be missing or might have default values.

Matched positionally or by name. When an argument is matched by name, it is ‘taken out’, and the remaining arguments are matched in their order.

Function arguments can be partially matched. Checks for exact match, then partial match, then positional match.

Lazy Evaluation

Arguments are only evaluated as needed.

## [1] 4

No error not specifying b as it’s not used.

The ‘…’ Argument.

Indicates a variable number of arguments often passed to other functions.

## [1] NA
## [1] 2

Also used when the number of arguments can’t be known:

## function (..., sep = " ", collapse = NULL) 
## NULL

Of course args after the dots must be named and cannot be partially matched.

Scoping Rules

R searches through environments to find a symbol:

## [1] ".GlobalEnv"        "package:stats"     "package:graphics" 
## [4] "package:grDevices" "package:utils"     "package:datasets" 
## [7] "package:methods"   "Autoloads"         "package:base"

The global environment is searched first, with all other packages searched in the order of the search() list.

When a user loads a package with library(), the namespace (environment) of the package is placed into position 2.

R has separate namespaces for functions and non-functions, so it’s possible to have an object and a function named c.

R uses lexical scoping or static scopring.

Consider the following function:

z is a free variable. What value is assigned to z?

Lexical Scoping - variables are seaerched for in the environment in which the function is defined.

If it’s not found, the search is continued in the parent environment.

After the top level environment, the search continues down the search list until we hit the empty environment.

Lexical vs Dynamic

With lexical scoping, the value is looked up in the environment in which the function was defined.

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

Often functions are defined and called in the global environment, making it seem as if the scoping is dynamic.

Lexical Consequences

All kfunctions must carry a pointer to their respective defining environments, which could be anywere.

Scoping Optimisation

There are some statistical optimisation functions like optim(), nlm() and optimize() that you pass a function to.

However an object may depend on a host of other things besides its parameters - like data.

We write a constructor function which constructs the objective function.

Close over the data you want to carry around, then call the constructed function with the variable parameters.

## function(p) {
##         params[!fixed] <- p
##         params
##     }
## <environment: 0x55a61cbf80c0>
## [1] "data"   "fixed"  "params"

Coding Standards

  1. Always use text files / text editor.
  2. Indent your code.
    • Can use Ctrl + I in RStudio
  3. Limit the width of your code (80 columns)
  4. Limit the length of individual functions.

Dates and Times

Dates are represented by the Date class, times by the POSIXct class.

Dates are stored as the number of days since 1970-01-01, times the number of seconds since 1970-01-01.

## [1] 5230
## [1] "Friday"
## [1] "April"
## [1] "Q2"

POSIXct is just a very large integer under the hood.

POSIXlt is a list underneath that stores other useful information.

## [1] "2019-09-26 14:05:50 AEST"
## [1] "POSIXct" "POSIXt"
## [1] "Thursday"
## [1] 1569470751

Use strptime() to create dates in a different format.

## [1] TRUE
## Time difference of 10860.17 days

Takes account of leap years, leap seconds, and timezones.

## Time difference of 0 secs
## Time difference of -11 hours