mply_dbl: apply function that always returns a numeric matrix

Description Usage Arguments Value Functions Examples

View source: R/util.R

Description

The function is modeled after 'vapply', but always returns a matrix with one row for each iteration. You need to provide the number of elements each function call produces beforehand (i.e. the number of resulting columns). For a more flexible version where you don't need to provide the number of columns see msply_dbl

Usage

1
2
3
mply_dbl(x, FUN, ncol = 1, ...)

msply_dbl(x, FUN, ...)

Arguments

x

a vector that will be passed to 'vapply' or a matrix that will be passed to apply with MARGIN=1.

FUN

the function that returns a vector of length ncol

ncol

the length of the vector returned by 'FUN'.

...

additional arguments to FUN

Value

a matrix of size length(x) x ncol

Functions

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
  # Behaves similar to sapply(), but it always returns a matrix
  t(sapply(1:5, function(i) c(i - i/3, i, i + i/3)))
  proDA:::mply_dbl(1:5, function(i) c(i - i/3, i, i + i/3), ncol=3)

  # Which can avoid some bad surprises
  t(sapply(1:5, identity))
  proDA:::mply_dbl(1:5, identity)


  # Works also with matrix input
  mat <- matrix(1:20, ncol=4)
  mat
  proDA:::msply_dbl(mat, function(i) rep(i, each=2))

proDA documentation built on Nov. 8, 2020, 5:01 p.m.