purrr: Functional Programming Tools

関数型プログラミングツール

> library(purrr)

Attaching package: 'purrr'
The following object is masked from 'package:testthat':

    is_null
The following objects are masked from 'package:git2r':

    is_empty, when
The following object is masked from 'package:magrittr':

    set_names

バージョン: 0.2.2.9000


関数名 概略
accumulate Accumulate recursive folds across a list
along Helper to create vectors with matching length.
array-coercion Coerce array to list
as_function Convert an object into a function.
as_vector Coerce a list to a vector
at_depth Map a function over lower levels of a nested list
bare-type-predicates Bare type predicates
by_row Apply a function to each row of a data frame
by_slice Apply a function to slices of a data frame
compose Compose multiple functions
conditional-map Modify elements conditionally
contains Does a list contain an object?
cross_n Produce all combinations of list elements
detect Find the value or position of the first match.
dmap Map over the columns of a data frame
every Do every or some elements of a list satisfy a predicate?
flatten Flatten a list of lists into a simple vector.
get-attr Infix attribute accessor
head_while Find head/tail that all satisfies a predicate.
invoke Invoke functions.
is_empty Is a vector/list empty?
is_formula Is a formula?
keep Keep or discard elements using a predicate function.
lift Lift the domain of a function
lmap Apply a function to list-elements of a list
map Apply a function to each element of a list.
map2 Map over multiple inputs simultaneously.
negate Negate a predicate function.
null-default Default value for 'NULL'.
partial Partial apply a function, filling in some arguments.
prepend Prepend a vector
rbernoulli Generate random samples from a Bernoulli distribution
rdunif Generate random samples from a discrete uniform distribution
reduce Reduce a list to a single value by iteratively applying a binary function.
rerun Re-run expressions multiple times.
safely Capture side effects.
scalar-type-predicates Scalar type predicates
set_names Set names in a vector
slice_rows Slice a data frame into groups of rows
splice Splice objects and lists of objects into a list
split_by Split, order and sort lists by their components.
transpose Transpose a list.
type-predicates Type predicates
update_list Modify a list
when Match/validate a set of conditions for an object and continue with the action associated with the first valid match.

accumulate / accumulate_right

> # 1, 1 + 2, 1 + 2 + 3
> 1:3 %>% accumulate(`+`)
[1] 1 3 6
> # 3, 3 * 2, 3 * 2 * 1
> 1:3 %>% accumulate_right(`*`)
[1] 6 6 3

along / list_along / rep_along

長さに応じて処理を実行

> x <- 1:5
> rep_along(x, 1:2)
[1] 1 2 1 2 1
> rep_along(x, 1)
[1] 1 1 1 1 1
> list_along(x)
[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

[[4]]
NULL

[[5]]
NULL

as_function

オブジェクトから関数を作成する

> as_function(~ . + 1)
Warning: `as_function()` is deprecated; please use `as_mapper()` or
`rlang::as_function()` instead
function (..., .x = ..1, .y = ..2, . = ..1) 
. + 1
<environment: 0x10d374980>
> as_function(c("a", "b", "c"))
Warning: `as_function()` is deprecated; please use `as_mapper()` or
`rlang::as_function()` instead
function (x, ...) 
isolate(x, list("a", "b", "c"), default = NULL)
<environment: 0x10dcd3460>

as_vector

ベクター型への強制変換

Arguments

  • .x
  • .type
> as.list(letters) %>% head(3)
[[1]]
[1] "a"

[[2]]
[1] "b"

[[3]]
[1] "c"
> as.list(letters) %>% as.vector("character")
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
[18] "r" "s" "t" "u" "v" "w" "x" "y" "z"
> as.list(letters) %>% as_vector("character")
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
[18] "r" "s" "t" "u" "v" "w" "x" "y" "z"
> as.list(letters) %>% as_vector(character(1))
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
[18] "r" "s" "t" "u" "v" "w" "x" "y" "z"

at_depth

ネスト構造を持つリスト型要素への関数の適用

ref) map

> l1 <- list(
+   obj1 = list(
+     prop1 = list(param1 = 1:2, param2 = 3:4),
+     prop2 = list(param1 = 5:6, param2 = 7:8)
+   ),
+   obj2 = list(
+     prop1 = list(param1 = 9:10, param2 = 11:12),
+     prop2 = list(param1 = 13:14, param2 = 15:16)
+   )
+ )
> str(l1)
List of 2
 $ obj1:List of 2
  ..$ prop1:List of 2
  .. ..$ param1: int [1:2] 1 2
  .. ..$ param2: int [1:2] 3 4
  ..$ prop2:List of 2
  .. ..$ param1: int [1:2] 5 6
  .. ..$ param2: int [1:2] 7 8
 $ obj2:List of 2
  ..$ prop1:List of 2
  .. ..$ param1: int [1:2] 9 10
  .. ..$ param2: int [1:2] 11 12
  ..$ prop2:List of 2
  .. ..$ param1: int [1:2] 13 14
  .. ..$ param2: int [1:2] 15 16
> l1 %>% at_depth(.depth = 3, .f = sum)
Warning: at_depth() is deprecated, please use `modify_depth()` instead
$obj1
$obj1$prop1
$obj1$prop1$param1
[1] 3

$obj1$prop1$param2
[1] 7


$obj1$prop2
$obj1$prop2$param1
[1] 11

$obj1$prop2$param2
[1] 15



$obj2
$obj2$prop1
$obj2$prop1$param1
[1] 19

$obj2$prop1$param2
[1] 23


$obj2$prop2
$obj2$prop2$param1
[1] 27

$obj2$prop2$param2
[1] 31
> l1 %>% at_depth(2, "param2") %>% str()
Warning: at_depth() is deprecated, please use `modify_depth()` instead
List of 2
 $ obj1:List of 2
  ..$ prop1: int [1:2] 3 4
  ..$ prop2: int [1:2] 7 8
 $ obj2:List of 2
  ..$ prop1: int [1:2] 11 12
  ..$ prop2: int [1:2] 15 16

by_row

データフレームの各行に関数を適用

> names(mtcars)
 [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
[11] "carb"
> mtcars %>% by_row(., sum) %>% {
+   print(names(.))
+   dplyr::tbl_df(.)
+ }
 [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
[11] "carb" ".out"
    mpg cyl  disp  hp drat    wt  qsec vs am gear carb    .out
1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4  328.98
2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4 329.795
3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1  259.58
4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1 426.135
5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2  590.31
6  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1  385.54
7  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4  656.92
8  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2  270.98
9  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2  299.57
10 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4  350.46
11 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4  349.66
12 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3  510.74
13 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3   511.5
14 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3  509.85
15 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4  728.56
16 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4 726.644
17 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4 725.695
18 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1  213.85
19 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2 195.165
20 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1 206.955
21 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1 273.775
22 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2  519.65
23 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2 506.085
24 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4  646.28
25 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2 631.175
26 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1 208.215
27 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2  272.57
28 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2 273.683
29 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4  670.69
30 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6  379.59
31 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8  694.71
32 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2  288.89
> data.frame(math      = c(70, 89),
+            chem      = c(45, 70),
+            row.names = c("Sato", "Tanaka")) %>% 
+   by_row(sum)
  math chem .out
1   70   45  115
2   89   70  159

by_slice

> mtcars %>%
+   slice_rows("cyl") %>%
+   by_slice(partial(lm, mpg ~ disp))
  cyl
1   4
2   6
3   8
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    .out
1                                                                                                                                                                                                                                                                                             40.8719553217, -0.13514181456, -3.47663934922, 3.35334887424, 0.956012168339, 2.16370548419, -0.241719959491, 2.63662769353, -3.14142339304, -2.89575197144, 1.38560496987, 2.38003124297, -3.11979575994, -88.4332773647, 11.4837351954, 0.60506028228, 3.86445043004, 1.55814077534, 4.58846597147, -2.80847633513, -1.20491860449, 1.71194430851, 3.53894320905, -2.81658343873, 2, 26.2766393492, 21.0466511258, 21.8439878317, 30.2362945158, 30.6417199595, 31.2633723065, 24.641423393, 30.1957519714, 24.6143950301, 28.019968757, 24.5197957599, 0, 1, -3.31662479036, 0.301511344578, 0.301511344578, 0.301511344578, 0.301511344578, 0.301511344578, 0.301511344578, 0.301511344578, 0.301511344578, 0.301511344578, 0.301511344578, -348.697870004, -84.9754403022, 0.411886530274, -0.318912860458, -0.354217178851, -0.408350467053, 0.168286733364, -0.315382428618, 0.17064035459, -0.12591591991, 0.178878028881, 1.30151134458, 1.48131835645, 1, 2, 0.0000001, 2, 9, lm(formula = mpg ~ disp, data = ..1), mpg ~ disp, 22.8, 24.4, 22.8, 32.4, 30.4, 33.9, 21.5, 27.3, 26, 30.4, 21.4, 108, 146.7, 140.8, 78.7, 75.7, 71.1, 120.1, 79, 120.3, 95.1, 121
2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                19.0819874187, 0.00360511850745, 1.34119362006, 1.34119362006, 1.38789200633, -1.79313908292, -0.486205280597, -1.8862052806, 0.0952703976712, -52.2346901699, 0.367025650487, 1.68471102534, -1.76685345775, -0.930483801053, -2.33048380105, -0.534282507651, 2, 19.6588063799, 19.6588063799, 20.0121079937, 19.8931390829, 19.6862052806, 19.6862052806, 19.6047296023, 0, 1, -2.64575131106, 0.377964473009, 0.377964473009, 0.377964473009, 0.377964473009, 0.377964473009, 0.377964473009, -485.004011765, 101.806819867, -0.796416524667, -0.472273210465, 0.0915397057518, 0.0915397057518, 0.313528763356, 1.37796447301, 1.16619089326, 1, 2, 0.0000001, 2, 5, lm(formula = mpg ~ disp, data = ..1), mpg ~ disp, 21, 21, 21.4, 18.1, 19.2, 17.8, 19.7, 160, 160, 258, 225, 167.6, 167.6, 145
3 22.0327989137, -0.0196340949126, 3.7354752549, -0.664524745103, -0.217715536747, 0.682284463253, -1.41771553675, -2.36550611489, -2.60111525384, 1.30620284791, -0.289156731434, -0.864034060211, -1.86086569423, 5.0208390514, 0.658768400683, -1.12293634495, -56.4990265403, 4.79764964832, -1.46339763598, -0.563397635978, -2.66339763598, -2.47048153071, -2.77585866253, 1.0151794511, -1.28948805574, -1.9457613762, -2.67514903755, 4.49725567836, -0.149700943236, -2.22210565916, 2, 14.9645247451, 14.9645247451, 16.6177155367, 16.6177155367, 16.6177155367, 12.7655061149, 13.0011152538, 13.3937971521, 15.7891567314, 16.0640340602, 15.1608656942, 14.1791609486, 15.1412315993, 16.1229363449, 0, 1, -3.74165738677, 0.267261241912, 0.267261241912, 0.267261241912, 0.267261241912, 0.267261241912, 0.267261241912, 0.267261241912, 0.267261241912, 0.267261241912, 0.267261241912, 0.267261241912, 0.267261241912, 0.267261241912, -1321.17922327, -244.352982384, -0.322300905083, -0.322300905083, -0.322300905083, 0.480635887771, 0.431526603805, 0.349677797194, -0.149599923134, -0.206894087762, -0.0186418325562, 0.185980183972, -0.0145493922256, -0.219171408753, 1.26726124191, 1.02228257075, 1, 2, 0.0000001, 2, 12, lm(formula = mpg ~ disp, data = ..1), mpg ~ disp, 18.7, 14.3, 16.4, 17.3, 15.2, 10.4, 10.4, 14.7, 15.5, 15.2, 13.3, 19.2, 15.8, 15, 360, 360, 275.8, 275.8, 275.8, 472, 460, 440, 318, 304, 350, 400, 351, 301
> library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following object is masked from 'package:testthat':

    matches
The following object is masked from 'package:git2r':

    pull
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
> mtcars %>% by_slice(map, mean)
Error: .d must be a sliced data frame
> # dplyr::summarise_eachと同じ結果
> mtcars %>% dplyr::summarise_each(funs(mean(.)))
Warning: `summarise_each()` is deprecated.
Use `summarise_all()`, `summarise_at()` or `summarise_if()` instead.
To map `funs` over all variables, use `summarise_all()`
        mpg    cyl       disp       hp      drat      wt     qsec     vs
1 20.090625 6.1875 230.721875 146.6875 3.5965625 3.21725 17.84875 0.4375
       am   gear   carb
1 0.40625 3.6875 2.8125

contains

リスト内にオブジェクトが存在するかどうかの確認

> list(1:10, 5, 9.9) %>% {
+   print(contains(., 1:10))
+   contains(., 3)
+ }
Error: is_string(match) is not TRUE
> x <- list(1:10, 5, 9.9)
> x %>% contains(1:10)
Error: is_string(match) is not TRUE
> x %>% contains(3)
Error: is_string(match) is not TRUE

conditional-map

> str(iris) # Species列のみがfactor
'data.frame':    150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
> iris %>% map_if(is.factor, as.character) %$% str(Species)
Warning: map_if() is deprecated, please use `modify_if()` instead
 chr [1:150] "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" ...
> # iris の変数に対し、is.factor = TRUEであればas.characterを適用する、という処理

cross

invoke

> list(m1 = mean, m2 = median) %>% invoke(rcauchy(100))
Error in do.call(.f, args, envir = .env): 'what' must be a function or character string

is_formula

> x <- disp ~ am
> is_formula(x)
[1] TRUE

lmap

map / map_lgl / map_chr / map_int / map_dbl / walk

リストの各要素に対して関数の処理を施す

> set.seed(123)
> (res_map <- map(1:3, rnorm, n = 4))
[[1]]
[1] 0.439524353448 0.769822510517 2.558708314149 1.070508391425

[[2]]
[1] 2.129287735161 3.715064986883 2.460916205989 0.734938765393

[[3]]
[1] 2.31314714811 2.55433802990 4.22408179744 3.35981382706
> set.seed(123)
> equals(res_map[[1]], rnorm(4, 1))
function (x) 
expect_equal(x, expected, ..., expected.label = label)
<environment: 0x10570c108>
> res <- list()
> mtcars %>%
+   split(.$cyl) %>%
+   map(~lm(mpg ~ wt, data = .)) %>%
+   map(summary) %>% {
+     res <<- .
+     map_dbl(., "r.squared")
+   }
             4              6              8 
0.508632596323 0.464510150551 0.422965536496
> c(res$`4`$r.squared, res$`6`$r.squared, res$`8`$r.squared)
[1] 0.508632596323 0.464510150551 0.422965536496

partial

rbernoulli

ベルヌーイ分布からランダムサンプリング

> rbernoulli(10, p = 0.5)
 [1] FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE

rerun

関数の処理を複数回繰り返す(listオブジェクトとして)

> 10 %>% rerun(rnorm(5))
[[1]]
[1]  1.715064986883  0.460916205989 -1.265061234607 -0.686852851894
[5] -0.445661970100

[[2]]
[1]  1.224081797439  0.359813827057  0.400771450594  0.110682715945
[5] -0.555841134754

[[3]]
[1]  1.786913136803  0.497850478229 -1.966617156630  0.701355901564
[5] -0.472791407728

[[4]]
[1] -1.067823705987 -0.217974914658 -1.026004448307 -0.728891229291
[5] -0.625039267849

[[5]]
[1] -1.686693310742  0.837787044495  0.153373117837 -1.138136937012
[5]  1.253814921070

[[6]]
[1]  0.426464221477 -0.295071482992  0.895125661045  0.878133487533
[5]  0.821581081637

[[7]]
[1]  0.6886402541001  0.5539176535376 -0.0619117105767 -0.3059626637399
[5] -0.3804710010124

[[8]]
[1] -0.694706978921 -0.207917278020 -1.265396351568  2.168955965339
[5]  1.207961998305

[[9]]
[1] -1.1231085832033 -0.4028848352991 -0.4666553536232  0.7799651183363
[5] -0.0833690664718

[[10]]
[1]  0.2533185139948 -0.0285467553487 -0.0428704572913  1.3686022840145
[5] -0.2257709856593
> 10 %>%
+   rerun(x = rnorm(5), y = rnorm(5)) %>%
+   map_dbl(~ cor(.x$x, .x$y))
 [1]  0.4778164038490 -0.2112033001716  0.3909316744848  0.7812810504088
 [5]  0.9253858048625  0.6631649073489 -0.5163835719661 -0.1626780531141
 [9]  0.0279612470224 -0.7173274836807

rdunif

一様分布に従う整数をランダムに生成する

Arguments

  • n... サンプルの数
  • a, b... 範囲
> rdunif(n = 10, b = 10, a = 1)
 [1] 4 5 8 1 4 9 9 3 4 9

slice_rows

dplyr::group_byと同等の機能を持つが、列名にグループ名を追加しない

> mtcars %>%
+   slice_rows("cyl") %>% head()
# A tibble: 6 x 11
# Groups:   cyl [3]
    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1  21.0     6   160   110  3.90 2.620 16.46     0     1     4     4
2  21.0     6   160   110  3.90 2.875 17.02     0     1     4     4
3  22.8     4   108    93  3.85 2.320 18.61     1     1     4     1
4  21.4     6   258   110  3.08 3.215 19.44     1     0     3     1
5  18.7     8   360   175  3.15 3.440 17.02     0     0     3     2
6  18.1     6   225   105  2.76 3.460 20.22     1     0     3     1
> mtcars %>%
+   dplyr::group_by("cyl") %>% head()
# A tibble: 6 x 12
# Groups:   "cyl" [1]
    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1  21.0     6   160   110  3.90 2.620 16.46     0     1     4     4
2  21.0     6   160   110  3.90 2.875 17.02     0     1     4     4
3  22.8     4   108    93  3.85 2.320 18.61     1     1     4     1
4  21.4     6   258   110  3.08 3.215 19.44     1     0     3     1
5  18.7     8   360   175  3.15 3.440 17.02     0     0     3     2
6  18.1     6   225   105  2.76 3.460 20.22     1     0     3     1
# ... with 1 more variables: `"cyl"` <chr>

split, unsplit

グループに分割・結合

> iris %>% split(.$Species) %>% map(head, 3)
$setosa
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa

$versicolor
   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
51          7.0         3.2          4.7         1.4 versicolor
52          6.4         3.2          4.5         1.5 versicolor
53          6.9         3.1          4.9         1.5 versicolor

$virginica
    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
101          6.3         3.3          6.0         2.5 virginica
102          5.8         2.7          5.1         1.9 virginica
103          7.1         3.0          5.9         2.1 virginica

transpose

> x <- rerun(5, x = runif(1), y = runif(5))
> x %>% str()
List of 5
 $ :List of 2
  ..$ x: num 0.854
  ..$ y: num [1:5] 0.2959 0.147 0.704 0.1038 0.0337
 $ :List of 2
  ..$ x: num 0.999
  ..$ y: num [1:5] 0.0349 0.3384 0.9151 0.6172 0.2863
 $ :List of 2
  ..$ x: num 0.738
  ..$ y: num [1:5] 0.834 0.314 0.493 0.697 0.641
 $ :List of 2
  ..$ x: num 0.644
  ..$ y: num [1:5] 0.978 0.415 0.119 0.526 0.225
 $ :List of 2
  ..$ x: num 0.486
  ..$ y: num [1:5] 0.37 0.983 0.388 0.229 0.623
> x %>% transpose() %>% str()
List of 2
 $ x:List of 5
  ..$ : num 0.854
  ..$ : num 0.999
  ..$ : num 0.738
  ..$ : num 0.644
  ..$ : num 0.486
 $ y:List of 5
  ..$ : num [1:5] 0.2959 0.147 0.704 0.1038 0.0337
  ..$ : num [1:5] 0.0349 0.3384 0.9151 0.6172 0.2863
  ..$ : num [1:5] 0.834 0.314 0.493 0.697 0.641
  ..$ : num [1:5] 0.978 0.415 0.119 0.526 0.225
  ..$ : num [1:5] 0.37 0.983 0.388 0.229 0.623
> x %>% transpose() %>% transpose() %>% str()
List of 5
 $ :List of 2
  ..$ x: num 0.854
  ..$ y: num [1:5] 0.2959 0.147 0.704 0.1038 0.0337
 $ :List of 2
  ..$ x: num 0.999
  ..$ y: num [1:5] 0.0349 0.3384 0.9151 0.6172 0.2863
 $ :List of 2
  ..$ x: num 0.738
  ..$ y: num [1:5] 0.834 0.314 0.493 0.697 0.641
 $ :List of 2
  ..$ x: num 0.644
  ..$ y: num [1:5] 0.978 0.415 0.119 0.526 0.225
 $ :List of 2
  ..$ x: num 0.486
  ..$ y: num [1:5] 0.37 0.983 0.388 0.229 0.623

update_list

リストの更新

> list(x = 1:10, y = 4) %>% {
+   str(.) %>% print()
+   update_list(., z = 10) %>% str() %>% print()
+ }
List of 2
 $ x: int [1:10] 1 2 3 4 5 6 7 8 9 10
 $ y: num 4
NULL
List of 3
 $ x: int [1:10] 1 2 3 4 5 6 7 8 9 10
 $ y: num 4
 $ z: num 10
NULL

when

> iris %>% subset(Sepal.Length > 10) %>%
+   when(nrow(.) > 0 ~ ., ~ iris %>% head(10))
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1           5.1         3.5          1.4         0.2  setosa
2           4.9         3.0          1.4         0.2  setosa
3           4.7         3.2          1.3         0.2  setosa
4           4.6         3.1          1.5         0.2  setosa
5           5.0         3.6          1.4         0.2  setosa
6           5.4         3.9          1.7         0.4  setosa
7           4.6         3.4          1.4         0.3  setosa
8           5.0         3.4          1.5         0.2  setosa
9           4.4         2.9          1.4         0.2  setosa
10          4.9         3.1          1.5         0.1  setosa
> 1:5 %>%
+   when(
+     sum(.) <=  50 ~ sum(.),
+     sum(.) <= 100 ~ sum(.) / 2,
+     ~ 0
+   )
[1] 15
> 1:10 %>%
+   when(
+     sum(.) <=  50 ~ sum(.),
+     sum(.) <= 100 ~ sum(.) / 2,
+     ~ 0
+   )
[1] 27.5

zip_n

> x <- rerun(5, x = runif(1), y = runif(5))
> x %>% str()
List of 5
 $ :List of 2
  ..$ x: num 0.137
  ..$ y: num [1:5] 0.967 0.515 0.163 0.622 0.986
 $ :List of 2
  ..$ x: num 0.669
  ..$ y: num [1:5] 0.419 0.323 0.835 0.144 0.193
 $ :List of 2
  ..$ x: num 0.897
  ..$ y: num [1:5] 0.3081 0.3633 0.7839 0.1934 0.0178
 $ :List of 2
  ..$ x: num 0.407
  ..$ y: num [1:5] 0.483 0.422 0.343 0.866 0.455
 $ :List of 2
  ..$ x: num 0.534
  ..$ y: num [1:5] 0.964 0.775 0.209 0.309 0.971
> x %>% zip_n() %>% str()
Error in zip_n(.): could not find function "zip_n"
> x %>% zip_n() %>% zip_n() %>% str()
Error in zip_n(.): could not find function "zip_n"
> x %>% zip_n() %>% zip_n() %>% zip_n() %>% str()
Error in zip_n(.): could not find function "zip_n"
> x %>% zip_n(.simplify = TRUE) %>% str()
Error in zip_n(., .simplify = TRUE): could not find function "zip_n"