map_along_dim(X, dim, func)
is a simple wrapper around split_along_dim(X, dim) %>% map(func)
. It is conceptually and functionally equivalent to
base::apply()
, with the following key differences:
it is guaranteed to return a list (base::apply()
attempts to simplify the
output to an array, sometimes unsuccessfully, making the output unstable)
it accepts the compact lambda notation ~.x
just like in purrr::map
(and modify_along_dim()
)
map_along_dim(X, .dim, .f, ...) map_along_rows(X, .f, ...) map_along_cols(X, .f, ...)
X | an R array |
---|---|
.dim | which dimension to map along. Passed on to
|
.f | A function, string of a function name, or |
... | passed on to |
An R list
X <- matrix2(letters[1:15], ncol = 3) apply(X, 1, function(x) paste(x, collapse = "")) # simplifies to a vector#> [1] "abc" "def" "ghi" "jkl" "mno"#> [[1]] #> [1] "abc" #> #> [[2]] #> [1] "def" #> #> [[3]] #> [1] "ghi" #> #> [[4]] #> [1] "jkl" #> #> [[5]] #> [1] "mno" #>#> [1] TRUE#> [1] TRUE