forked from YouSayData/DSR_Intermediate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloops_maps.R
136 lines (94 loc) · 3.23 KB
/
loops_maps.R
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
131
132
133
134
135
136
# Libraries ---------------------------------------------------------------
library(tidyverse)
# Loops -------------------------------------------------------------------
# Remember our tibble from functions.R
# Let's put the creation of it in a function itself
newData <- function() {
tibble(
a = rnorm(10),
b = rnorm(10),
c = rnorm(10),
d = rnorm(10)
)
}
# If we want to calculate the median for each column we could do
# it in multiple ways
# Four steps
df <- newData()
median(df$a)
median(df$b)
median(df$c)
median(df$d)
# This seems bulky. Let's use a for-loop instead
output <- vector("double", ncol(df)) # 1. output
for (i in seq_along(df)) { # 2. sequence
output[[i]] <- median(df[[i]]) # 3. body
}
output
# Exercise I --------------------------------------------------------------
# Write loops for (Think about the output, sequence, and body before you start writing the loop.):
# 1. Compute the mean of every column in mtcars.
# 2. Determine the type of each column in nycflights13::flights.
# 3. Compute the number of unique values in each column of iris.
# 4. Generate 10 random normals from distributions with means of -10, 0, 10, and 100.
# Modifying an existing object with a for loop ----------------------------
df <- newData()
rescale01 <- function(x) {
rng <- range(x, na.rm = TRUE)
(x - rng[1]) / (rng[2] - rng[1])
}
for (i in seq_along(df)) {
df[[i]] <- rescale01(df[[i]])
}
# While Loop --------------------------------------------------------------
# Good for observing events in web development
# or simulations
roleDice <- function() sample(1:6, 1)
playLotto <- function() sample(1:40, 6)
pickNumbers <- function() sample(1:40, 6)
weeks <- 1
hits <- 0
myNumbers <- pickNumbers()
while (hits < 5) {
hits <- (myNumbers %in% playLotto()) %>% sum
weeks <- weeks + .5
}
str_c("You got 5 hits after playing for over",
floor(weeks / 52),
"years.", sep = " ")
# Exercise II -------------------------------------------------------------
# 1. Write a function that prints the mean of each numeric column in a data frame,
# along with its name. Test it on the iris data set.
# purrr maps --------------------------------------------------------------
# map() makes a list.
# map_lgl() makes a logical vector.
# map_int() makes an integer vector.
# map_dbl() makes a double vector.
# map_chr() makes a character vector.
df <- newData()
map_dbl(df, mean)
map_dbl(df, median)
map_dbl(df, sd)
# Or in a pipe
df %>% map_dbl(mean)
df$a[2] <- NA
df
df %>% map_dbl(mean)
map_dbl(df, mean, na.rm = T)
# Shortcuts ---------------------------------------------------------------
# this is verbose
mod_coef <- mtcars %>%
split(.$cyl) %>%
map(function(df) lm(mpg ~ wt, data = df)) %>%
map(coef)
# less verbose
mod_coef <- mtcars %>%
split(.$cyl) %>%
map(~lm(mpg ~ wt, data = .)) %>%
map(coef)
# Exercises ---------------------------------------------------------------
# Write code that uses one of the map functions to:
# 1. Compute the mean of every column in mtcars.
# 2. Determine the type of each column in nycflights13::flights.
# 3. Compute the number of unique values in each column of iris.
# 4. Generate 10 random normals from distributions with means of -10, 0, 10, and 100.