# 1 R Syntax

Key syntax and operators.

## 1.1 Basic operators

Action Operator Example
Subtract - 5 - 4 = 1
Add + 5 + 4 = 9
Multiply * 5 * 4 = 20
Divide / 5 / 4 = 1.25
Raise to the power ^ 5 ^ 4 = 625
Modulus %% 9 %% 4 = 1
Integer division %/% 9 %/% 4 = 2
Basic sequence : 1:3 = 1, 2, 3

## 1.2 Comparison operators

Action Operator Example
Less than < 5 < 5 = FALSE
Less than or equal to <= 5 <= 5 = TRUE
Greater than > 5 > 5 = FALSE
Greater than or equal to >= 5 >= 5 = TRUE
Equal all.equal() all.equal(0.5 - 0.3,0.3 - 0.1) is TRUE
Exactly equal == (0.5 - 0.3) == (0.3 - 0.1) is FALSE, 2 == 2 is TRUE
Not equal != (0.5 - 0.3) != (0.3 - 0.1) is TRUE, 2 != 2 is FALSE

## 1.3 States

States Representation
True TRUE 1
False FALSE 0
Empty NULL
Unknown NA
Not a number e.g. 0/0 NaN
Infinite e.g. 1/0 Inf

## 1.4 Logical operators

Action Operator Example
Not ! !TRUE is FALSE
And & TRUE & FALSE is FALSE, c(TRUE,TRUE) & c(FALSE,TRUE) is FALSE, TRUE
Or | TRUE | FALSE is TRUE, c(TRUE,FALSE) | c(FALSE,FALSE) is TRUE, FALSE
Xor xor() xor(TRUE,FALSE) is TRUE
Bitwise And && c(TRUE,TRUE) && c(FALSE,TRUE) is FALSE
Bitwise Or || c(TRUE,FALSE) || c(FALSE,FALSE) is TRUE
In %in% "Red" %in% c("Blue","Red") is TRUE
Not in !( x %in% y) !("Red" %in% c("Blue","Red")) = FALSE

## 1.5 Control constructs

Type Implementation Example
If if(condition) {dosomething} if(TRUE) { 2 } is 2
If else if(condition) {do something} else {do something different} or ifelse(condition, do something, do something else) if(FALSE) { 2 } else { 3 } is 3 ifelse(FALSE, 2, 3) is 3
For loop for(i in seq) {dosomething} or foreach(i=1:3) %do% {something} for(i in 1:3) {print(i)} is 1, 2, 3
While loop while(condition) {do something } a<-0 ; while(a<3){a<-a+1} ; a is 3
Switch switch(value, …) switch(2, "a", "b") is b
Case memisc::cases(…) cases("pi<3"=pi<3, "pi=3"=pi==3,"pi>3"=pi>3) is pi>3

NB: If you find yourself using a loop, there’s probably a better, faster solution

## 1.6 Assignment operators

Action Operator Example
Create / update a variable <- a <- 10

NB: There are others you could use, but this is the best practice

## 1.7 Accessors

Action Operator Example
Use public function from package :: memisc::cases()
Use private function from package ::: optiRum:::pounds_format()
Get a component e.g a data.frame column $iris$Sepal.Length
Extract a property from a class @ Won’t be used in this course
Refer to positions in a data.frame or vector [ ] iris[5:10,1]
Refer to item in a list [[ ]] list(iris=iris,mtcars=mtcars)[["iris"]]

## 1.8 Meta-operators

Action Operator Example
Comment # # This is my comment
Help ? ?data.table
Identifier  1<-2

## 1.9 Exercises

1. Create a variable called Scores that holds the numbers 1 to 10
2. Return whether each number in Scores is an even number
3. Get the help text for the function mean
4. Perform a test to see if R variable names are case sensitive

#1
Scores<-1:10
#2
(Scores %% 2) ==0
##   FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
#3
#?mean
#4
scores
## Error in eval(expr, envir, enclos): object 'scores' not found

# 2 R Data types & structures

A brief introduction to data structures and types

## 2.1 Data types

These are the core data types. There are additonal ones like dates with timestamps(POSIXct and POSIXlt) and ordered factors

Data type Example
Integer 1
Logical TRUE
Numeric 1.1
String / character “Red”
Factor (enumerated string) “Amber” or 2 in c(“Red”,“Amber”,“Green”)
Complex i
Date “2016-05-19”

## 2.2 Data structures

These are the out of the box data structures. There are other data structure that use these as the basis like data.table or a time-series object.

Data type Info Construction example(s)
Vector A 1D set of values of the same data type c(1,“a”) , 1:3 , LETTERS
Matrix A 2D set of values of the same data type matrix(LETTERS,nrow=13, ncol=2) , rbind(1:5,2:6)
Array An nD set of values of the same data type array(LETTERS, c(13,2))
Data.frame A 2D set of values of different data types data.frame(a=1:26, b=LETTERS)
List A collection of objects of various data types list(vector=c(1,“a”), df=data.frame(a=1:6))
Classes A class is like a formalised list and can also contain functions i.e. methods Won’t be covered in this class

## 2.3 Exercises

1. Make a variable called ID containing the numbers 1 to 52
2. Make a variable called category holding 52 randomly selected letters (using sample)
3. Make a variable called records that is a data.frame containing ID and category

#1
ID<-1:52
#2
category<-sample(LETTERS,52, replace=TRUE)
#3
records<-data.frame(ID,category)
head(records)
##   ID category
## 1  1        L
## 2  2        W
## 3  3        L
## 4  4        K
## 5  5        I
## 6  6        I

# 3 R Functions

• Use an argument’s position for well known functions e.g. sum(iris$Sepal.Width) • Use argument names for less well known functions (or less common arguments) e.g. seq(from=1, to=5, by=1) • Use argument names to call arguments out of order e.g.seq(by=1, from=1, to=5 ) • Get help for a function that’s in a loaded package with ? e.g. ?mean • Get help for a function that’s not loaded with ?? e.g. ??adist • Search for text or a function in the help with help.search() e.g. help.search("concat") • Get the definition of a function by calling it without brackets e.g. Sys.Date ## 3.2 Functions can … • have named arguments e.g. ifelse(test, yes, no) • take any number of arguments e.g. mean(...) • take a combination of named and unlimited arguments e.g. seq.int(from, to, by, length.out, along.with, ...) • act as operators e.g. +,%%,%>% • work as you’d would normally expect e.g. mean(iris$Sepal.Width)
• return functions e.g. scales::gradient_n_pal
• have side-effects like making a plot and also returning some data

## 3.3 Basic function writing

• Functions are created with the syntax function(args){code}
• Functions do not need to be stored to be used
• Store a function with myfunc<-function(args){code}
• Functions will usually output whatever object was last called but not stored f<-function(){1}; f()
• Use the return() function to output something specific f<-function(){return(1)}; f()
• Make an argument optional by providing it with a default value or NULL
myfunc<-function(arg1, arg2=NULL){
print(arg1)
if(!is.null(arg2)) return(arg2)
}

## 3.4 Exercises

1. Use the function cut() to produce split floor(rnorm(n = 100, mean=50,sd=20)) into 5 bins with numbers representing the bins they were allocated
2. Write a function that says “Hello world” or says hello to any string or number passed in (use paste)

#1
nums<-floor(rnorm(n = 100, mean=50,sd=20))
bins<-cut(nums, breaks=5, labels=FALSE)

#2
hello<-function(x=NULL){
if(is.null(x)) x<-"world"
res<-paste0("Hello ", x, "!")
return(res)
}
hello()
##  "Hello world!"
hello("Dave")
##  "Hello Dave!"
hello(c("Dave","Jane", "7"))
##  "Hello Dave!" "Hello Jane!" "Hello 7!"`