Key syntax and operators.
Action | Operator | Example
------------- | ------------- | -------------
Subtract | - | 5 - 4
= r 5 - 4
Add | + | 5 + 4
= r 5 + 4
Multiply | * | 5 * 4
= r 5 * 4
Divide | / | 5 / 4
= r 5 / 4
Raise to the power | ^ | 5 ^ 4
= r 5 ^ 4
Modulus | %% | 9 %% 4
= r 9 %% 4
Integer division | %/% | 9 %/% 4
= r 9 %/% 4
Basic sequence | : | 1:3
= r 1:3
Action | Operator | Example
------------- | ------------- | -------------
Less than | < | 5 < 5
= r 5 < 5
Less than or equal to | <= | 5 <= 5
= r 5 <= 5
Greater than | > | 5 > 5
= r 5 > 5
Greater than or equal to | >= | 5 >= 5
= r 5 >= 5
Equal | all.equal() | all.equal(0.5 - 0.3,0.3 - 0.1)
is r all.equal(0.5 - 0.3,0.3 - 0.1)
Exactly equal | == | (0.5 - 0.3) == (0.3 - 0.1)
is r (0.5 - 0.3) == (0.3 - 0.1)
, 2 == 2 is r 2==2
Not equal | != | (0.5 - 0.3) != (0.3 - 0.1)
is r (0.5 - 0.3) != (0.3 - 0.1)
, 2 != 2 is r 2!=2
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
Action | Operator | Example
------------- | ------------- | -------------
Not | ! | !TRUE
is r !TRUE
And | & | TRUE & FALSE
is r TRUE & FALSE
, c(TRUE,TRUE) & c(FALSE,TRUE)
is r c(TRUE,TRUE) & c(FALSE,TRUE)
Or | |
| TRUE | FALSE
is r TRUE | FALSE
, c(TRUE,FALSE) | c(FALSE,FALSE)
is r c(TRUE,FALSE) | c(FALSE,FALSE)
Xor | xor() | xor(TRUE,FALSE)
is r xor(TRUE,FALSE)
Bitwise And | && | c(TRUE,TRUE) && c(FALSE,TRUE)
is r c(TRUE,TRUE) && c(FALSE,TRUE)
Bitwise Or | ||
| c(TRUE,FALSE) || c(FALSE,FALSE)
is r c(TRUE,FALSE) || c(FALSE,FALSE)
In | %in% | "Red" %in% c("Blue","Red")
is r "Red" %in% c("Blue","Red")
Not in | !( x %in% y) | !("Red" %in% c("Blue","Red"))
= r !("Red" %in% c("Blue","Red"))
Type | Implementation | Example
------------- | ------------- | -------------
If | if(condition) {dosomething} | if(TRUE) { 2 }
is r if(TRUE) { 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 r if(FALSE) { 2 } else { 3 }
ifelse(FALSE, 2, 3)
is r ifelse(FALSE, 2, 3)
For loop | for(i in seq) {dosomething} or foreach(i=1:3) %do% {something} | for(i in 1:3) {print(i)}
is r paste0(1:3)
While loop | while(condition) {do something } | a<-0 ; while(a<3){a<-a+1} ; a
is r a<-0 ; while(a<3){a<-a+1} ; a
Switch | switch(value, ...) | switch(2, "a", "b")
is r switch(2, "a", "b")
Case | memisc::cases(...) | cases("pi<3"=pi<3, "pi=3"=pi==3,"pi>3"=pi>3)
is r memisc::cases("pi<3"=pi<3, "pi=3"=pi==3,"pi>3"=pi>3)
NB: If you find yourself using a loop, there's probably a better, faster solution
Action | Operator | Example
------------- | ------------- | -------------
Create / update a variable | <- | a <- 10
NB: There are others you could use, but this is the best practice
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"]]
Action | Operator | Example
------------- | ------------- | -------------
Comment | # | # This is my comment
Help | ? | ?data.table
Identifier | ` | `1`<-2
Scores
that holds the numbers 1 to 10mean
#1 Scores<-1:10 #2 (Scores %% 2) ==0 #3 #?mean #4 scores
A brief introduction to data structures and 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 | "r Sys.Date()
"
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
ID
containing the numbers 1 to 52category
holding 52 randomly selected letters (using sample
)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)
sum(iris$Sepal.Width)
seq(from=1, to=5, by=1)
seq(by=1, from=1, to=5 )
?
e.g. ?mean
??
e.g. ??adist
help.search()
e.g. help.search("concat")
Sys.Date
ifelse(test, yes, no)
mean(...)
seq.int(from, to, by, length.out, along.with, ...)
+
,%%
,%>%
mean(iris$Sepal.Width)
scales::gradient_n_pal
function(args){code}
myfunc<-function(args){code}
f<-function(){1}; f()
return()
function to output something specific f<-function(){return(1)}; f()
myfunc<-function(arg1, arg2=NULL){ print(arg1) if(!is.null(arg2)) return(arg2) }
cut()
to produce split floor(rnorm(n = 100, mean=50,sd=20))
into 5 bins with numbers representing the bins they were allocatedpaste
)#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("Dave") hello(c("Dave","Jane", "7"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.