Learn R Programming

rvec (version 0.0.7)

map_rvec: Apply a Function and Put Results in an Rvec

Description

Apply function .f to each element of .x, and then combine the results into an rvec with the same length as .x.

Usage

map_rvec(.x, .f, ...)

Value

An rvec with the same length as .x.

Arguments

.x

A vector.

.f

A function.

...

Additional arguments passed to .f.

Details

Each call to function .f should produce an rvec with length 1.

See Also

map_rvec() is based on the map functions in package purrr, though the internal implementation is different.

Base R functions sapply() and vapply() do not work properly with rvecs. [lapply() works, but to combine the results into a single rvec, functions such as c() or vctrs::vec_c() are needed.

Examples

Run this code
l <- list(a = rvec(matrix(1:2, 1)),
          b = rvec(matrix(1:4, 2)),
          c = rvec(matrix(1:6, 3)))
l
map_rvec(l, sum)

## sapply does not work with rvecs
sapply(l, sum)

Run the code above in your browser using DataLab