Home | Prev | Next
R Syntax
Key syntax and operators.
Basic operators
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 |
Comparison operators
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 |
States
True |
TRUE 1 |
False |
FALSE 0 |
Empty |
NULL |
Unknown |
NA |
Not a number e.g. 0/0 |
NaN |
Infinite e.g. 1/0 |
Inf |
Logical operators
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 |
Control constructs
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
Assignment operators
Create / update a variable |
<- |
a <- 10 |
NB: There are others you could use, but this is the best practice
Accessors
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"]] |
Exercises
- Create a variable called
Scores
that holds the numbers 1 to 10
- Return whether each number in Scores is an even number
- Get the help text for the function
mean
- Perform a test to see if R variable names are case sensitive
Answers
#1
Scores<-1:10
#2
(Scores %% 2) ==0
## [1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE
#3
#?mean
#4
scores
## Error in eval(expr, envir, enclos): object 'scores' not found
R Data types & structures
A brief introduction to data structures and types
Data types
These are the core data types. There are additonal ones like dates with timestamps(POSIXct
and POSIXlt
) and ordered factors
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” |
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.
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 |
Exercises
- Make a variable called
ID
containing the numbers 1 to 52
- Make a variable called
category
holding 52 randomly selected letters (using sample
)
- Make a variable called
records
that is a data.frame containing ID
and category
Answers
#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
R Functions
Using 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
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
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)
}
Exercises
- 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
- Write a function that says “Hello world” or says hello to any string or number passed in (use
paste
)
Answers
#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()
## [1] "Hello world!"
hello("Dave")
## [1] "Hello Dave!"
hello(c("Dave","Jane", "7"))
## [1] "Hello Dave!" "Hello Jane!" "Hello 7!"