Notes 04: More Functional Programming and Vectorization

Advanced R 2nd edition, Hadley Wickham, Chapter: Functional programming

Vectorization and Recycling

Element-wise operations

### devsify.ai
### Notes for Session04 More Functional Programming
v1 <- c(1,2,3,4,5,17)
print(v1)
## [1]  1  2  3  4  5 17
### devsify.ai
### Notes for Session04 More Functional Programming
a <- 5
print(a[1])
## [1] 5
print(a)
## [1] 5
### devsify.ai
### Notes for Session04 More Functional Programming
v1 <- 5
v2 <- c(2,3,4)
print(v1 + v2)
## [1] 7 8 9
### devsify.ai
### Notes for Session04 More Functional Programming
v1 <- c(1,2,3)
v2 <- c(2,3,4)
print(v1 + v2)
## [1] 3 5 7
### devsify.ai
### Notes for Session04 More Functional Programming
v1 <- c(1,2)
v2 <- c(2,3,4)
print(v1 + v2)
## [1] 3 5 5
### devsify.ai
### Notes for Session04 More Functional Programming
v1 <- 5
v2 <- c(2,3,4)
print(v2 + v1)
## [1] 7 8 9

Lapply(), Sapply(), Apply()

### devsify.ai
### Notes for Session04 More Functional Programming

v1 <- 1:100

# - power() function
power <- function(x) {
  # code blocks...
  return(x^2)
}

# - silly functional:
squares <- lapply(v1, power)
class(squares)
## [1] "list"
# - sapply():
squares <- sapply(v1, power)
class(squares)
## [1] "numeric"
# - apply
m1 <- matrix(data = c(1,2,3,4),
             ncol = 2)
sum(c(1,2))
## [1] 3
apply(X = m1, MARGIN = 1, FUN = sum)
## [1] 4 6
apply(X = m1, MARGIN = 2, FUN = sum)
## [1] 3 7

Reduce()

### devsify.ai
### Notes for Session04 More Functional Programming

Map()

### devsify.ai
### Notes for Session04 More Functional Programming

c1 <- c("Paris", "London")
c2 <- c("France", "UK")
m <- Map(paste, c1, c2)
class(m)
## [1] "list"
print(m)
## $Paris
## [1] "Paris France"
## 
## $London
## [1] "London UK"
# a binary function
f2 <- function(a, b) {
  return(
    a^2 + b
  )
}

v1 <- 1:20
v2 <- 21:40
m <- Map(f2, v1, v2)
print(m)
## [[1]]
## [1] 22
## 
## [[2]]
## [1] 26
## 
## [[3]]
## [1] 32
## 
## [[4]]
## [1] 40
## 
## [[5]]
## [1] 50
## 
## [[6]]
## [1] 62
## 
## [[7]]
## [1] 76
## 
## [[8]]
## [1] 92
## 
## [[9]]
## [1] 110
## 
## [[10]]
## [1] 130
## 
## [[11]]
## [1] 152
## 
## [[12]]
## [1] 176
## 
## [[13]]
## [1] 202
## 
## [[14]]
## [1] 230
## 
## [[15]]
## [1] 260
## 
## [[16]]
## [1] 292
## 
## [[17]]
## [1] 326
## 
## [[18]]
## [1] 362
## 
## [[19]]
## [1] 400
## 
## [[20]]
## [1] 440

Function Factories

### devsify.ai
### Notes for Session04 More Functional Programming

License: GPLv3 This Notebook is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This Notebook is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this Notebook. If not, see http://www.gnu.org/licenses/.


Contact: goran.milovanovic@datakolektiv.com

  

Impressum
Data Kolektiv, 2004, Belgrade.