String comparison

String comparison is based on alphabetical order

## [1] TRUE

Logical operators

## [1]  TRUE FALSE FALSE
## [1] TRUE
## [1] TRUE

Loops

## [1] "ctr is equal to 2"
## [1] "ctr is equal to 3"
## [1] "ctr is equal to 4"
## [1] "ctr is equal to 5"
## [1] "ctr is equal to 6"
## [1] "ctr is equal to 7"
## [1] "Your speed is 88"

For loops

## [1] "1 is odd"
## [1] "3 is odd"
## [1] "5 is odd"
## [1] "7 is odd"
## [1] "9 is odd"
## [1] "11 is odd"
## [1] "13 is odd"
## [1] "15 is odd"
## [1] 8405837
## [1] "Manhattan"     "Bronx"         "Brooklyn"      "Queens"       
## [5] "Staten Island"
## [1] FALSE
## [1] 8405837
## [1] "Manhattan"     "Bronx"         "Brooklyn"      "Queens"       
## [5] "Staten Island"
## [1] FALSE
## [1] "On row 1 and column 1 the board contains O"
## [1] "On row 1 and column 2 the board contains NA"
## [1] "On row 1 and column 3 the board contains X"
## [1] "On row 2 and column 1 the board contains NA"
## [1] "On row 2 and column 2 the board contains O"
## [1] "On row 2 and column 3 the board contains NA"
## [1] "On row 3 and column 1 the board contains X"
## [1] "On row 3 and column 2 the board contains O"
## [1] "On row 3 and column 3 the board contains X"
## [1] 5

Functions

Apply functions

Sapply (Simplify apply)

  • apply function over list or vector
  • try to simplify list to array

Instead of return a list, sapply returns a named vector

## [1] -1.3945295  1.1253849 -2.6295448  1.1234166 -0.6408276
## [[1]]
## [1] -1.394529
## 
## [[2]]
## [1] 1.125385
## 
## [[3]]
## [1] -2.629545
## 
## [[4]]
## [1] 1.123417
## 
## [[5]]
## [1] -0.6408276
## The average temperature is -1.394529 
## The average temperature is 1.125385 
## The average temperature is -2.629545 
## The average temperature is 1.123417 
## The average temperature is -0.6408276
## [[1]]
## NULL
## 
## [[2]]
## NULL
## 
## [[3]]
## NULL
## 
## [[4]]
## NULL
## 
## [[5]]
## NULL
## The average temperature is -1.394529 
## The average temperature is 1.125385 
## The average temperature is -2.629545 
## The average temperature is 1.123417 
## The average temperature is -0.6408276
## [[1]]
## NULL
## 
## [[2]]
## NULL
## 
## [[3]]
## NULL
## 
## [[4]]
## NULL
## 
## [[5]]
## NULL

Vapply

  • Apply function over list or vector
  • explicitly specify output format
##           [,1]     [,2]      [,3]     [,4]       [,5]
## min  -1.394529 1.125385 -2.629545 1.123417 -0.6408276
## mean -1.394529 1.125385 -2.629545 1.123417 -0.6408276
## max  -1.394529 1.125385 -2.629545 1.123417 -0.6408276

Useful functions

## [1]  1  4  7 10
## [1] 8 6 4 2
## [1] 8 6 4 2 8 6 4 2
## [1] 8 8 6 6 4 4 2 2
## [1] 8 8 6 6 4 4 2 2
## [1] FALSE
## [1] TRUE
## [1] 8 6 4 2
## [[1]]
## [1] 8
## 
## [[2]]
## [1] 6
## 
## [[3]]
## [1] 4
## 
## [[4]]
## [1] 2
## 
## [[5]]
## [1] 2
## 
## [[6]]
## [1] 4
## 
## [[7]]
## [1] 6
## 
## [[8]]
## [1] 8