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 | %% | 10 %% 4 = 2 |
Absolute remainder | %/% | 9 %/% 4 = 2 |
Basic sequence | : | sum(1:3) = 6 |
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 |
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 |
Equal | all.equal() | all.equal(0.5 - 0.3,0.3 - 0.1) is TRUE |
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 FALSE |
And | & | TRUE & FALSE is FALSE, c(TRUE,TRUE) & c(FALSE,TRUE) is FALSE, TRUE |
Or | ||
Xor | xor() | xor(TRUE,FALSE) is TRUE |
Bitwise And | && | c(TRUE,TRUE) && c(FALSE,TRUE) is FALSE |
Bitwise Or | ||
In | %in% | "Red" %in% c("Blue","Red") is TRUE |
Not in | !( x %in% y) or Hmisc::%nin% | "Red" %nin% c("Blue","Red") = FALSE |
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::foreach(i=1:3) %do% {something} | foreach(i=1:3) %do% {TRUE} is TRUE, TRUE, TRUE |
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
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 | irisDT[ , `:=`(CreatedDate = Sys.Date())] |