do.bind: List-apply a function and then bind the results

Description Usage Arguments Details Value See Also Examples

View source: R/do.bind.R

Description

List-apply a function and then bind the results. Effectively, this function calls lapply, do.call, and eitherrbind or cbind depending on the given specifications.

Usage

1
do.bind(f, x, m = 1, ...)

Arguments

f

A function to apply to the collection.

x

A collection, such as a list, matrix, or dataframe.

m

Margin. 1 to call rbind (default) and 2 for cbind.

...

Arguments passed to do.call.

Details

After applying functions such as lapply or Map to a dataset, do.call is often a solution to combine the list elements to obtain the original tabular format.

To simplify this process, lapply anddo.call are wrapped into do.bind: it calls a function on a dataset and binds the elements into a matrix or dataframe (depending on the original collection type). One may achieve similar results with purrr::map_dfr or purrr::map_dfc.

The type of bind is defined in a binary fashion (1 for row-wise, 2 for column-wise).

Value

Matrix or dataframe.

See Also

https://github.com/robertschnitman/afp, lapply, do.call, rbind, cbind, https://purrr.tidyverse.org/

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 1. Store lm() coefficients in matrix.
split1  <- split(mtcars, mtcars$gear)
adhoc1  <- function(s) {coef(lm(mpg ~ disp + wt + am, s))}
output1 <- do.bind(adhoc1, split1)
output1 # matrix. Rownames indicate subset.

# 2. What is the median Ozone-Temperature ratio for each given month?
airquality2 <- na.omit(airquality)
split2      <- split(airquality2, airquality2$Month)
adhoc2      <- function(x, y) {median(x/y)}
output2     <- do.bind(function(s) with(s, adhoc2(Ozone, Temp)), split2, 2)
output2 # matrix

robertschnitman/afp documentation built on Aug. 9, 2020, 9:46 a.m.