-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution_14.R
More file actions
29 lines (20 loc) · 767 Bytes
/
Copy pathSolution_14.R
File metadata and controls
29 lines (20 loc) · 767 Bytes
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
# Solution 14
library(tidyverse)
# here's a quick look at reduce, accumulate and flatten. these functions aren't
# as useful (in my opinion/experience!) as the map functions, but they do have
# their uses.
values <- 1:10
# try using reduce with any binary function, for example `+`, or min
# (note the backticks with the special infix functions +, -, *, /)
# try replacing reduce with accumulate to see the results
reduce(values, `+`)
accumulate(values, `+`)
# now, let's have a look at flatten. first, let's generate a list of random
# values
set.seed(134)
values <- map(values, ~sample(1:10, .x))
values
# now, try to flatten this to a single vector of doubles
flatten(values)
flatten_dbl(values)
# see what happens with the flatten function vs flatten_dbl