-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplyExample.Rmd
More file actions
130 lines (103 loc) · 2.72 KB
/
Copy pathapplyExample.Rmd
File metadata and controls
130 lines (103 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
---
title: "apply example"
author: "Brian S. Yandell"
date: "7/5/2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
The purpose of the base `apply` family of functions is to SIMPLIFY loops.
## loop approach
```{r}
numSimulations <- 10000
ptm <- proc.time()
results <- NULL
for(iS in 1:numSimulations){
temp <- rnorm(100, 0, 1)
results <- rbind(results,
c(simNumber = iS,
mean = mean(temp),
min = min(temp),
max = max(temp)))
}
(loopTime <- proc.time() - ptm)
```
## apply approach - "apply" function
```{r}
ptm <- proc.time()
simulation <- function(ID){
temp <- rnorm(100, 0, 1)
c(simNumber = ID,
mean = mean(temp),
min = min(temp),
max = max(temp))
}
results <- sapply(1:numSimulations, simulation)
results <- t(results)
(applyTime <- proc.time() - ptm)
```
But this is misleading; the bottleneck in the loop
is that the array is being continuously copied and
memory reallocated. If instead we allocate the memory
initially, then the times become quite close.
```{r}
ptm <- proc.time()
results <- matrix(0, nrow = numSimulations, ncol = 4)
for(iS in 1:numSimulations){
temp <- rnorm(100, 0, 1)
results[iS,] <- c(simNumber = iS,
mean = mean(temp),
min = min(temp),
max = max(temp))
}
(loopTime2 <- proc.time() - ptm)
```
### lapply and sapply
`lapply` takes a vector or list (incl. data.frame) and returns list.
`sapply` does the same but returns a simplified structure.
```{r}
myList <- list(a = c(1,2,3,4,5),
b = c(6,7,8,9,10),
c = c(11,12,13,14,15))
fun_item1 <- function(data1, data2){ data1 }
fun_type1 <- function(data1) { list(typeof(data1), data1) }
lapply(myList, mean)
sapply(myList, mean)
myVector <- 0:10
lapply(myVector, `^`, 2)
sapply(myVector, `^`, 2)
myDF <- data.frame(names = c("ann","bob","corinne"),
salary = c(15000, 25000, 150000),
age = c(50, 40, 60))
lapply(myDF, mean)
sapply(myDF, mean)
```
### vapply
`vapply` works on vectors, returns an array, requires FUN.VALUE
```{r}
myStats <- function(data){
c(mean=mean(data), min=min(data), max=max(data))
}
vapply(myList, myStats,
FUN.VALUE = c(Avg = 0, minValue = 0, maxValue = 0))
```
### apply
`apply` works on the margin of an array
```{r}
myArray <- matrix(1:12, ncol=3)
apply(myArray, 1, sum)
apply(myArray, 2, sum)
```
### tapply
`tapply` uses factors instead of margins
```{r}
myVector <- 1:12
myFactors <- rep(c("a","b"), c(5,7))
tapply(myVector, myFactors, min)
```
### mapply
`mapply` works where functions require more than one argument
```{r}
mapply(`^`, 1:4, 2:5)
```