Description Usage Arguments Details Value See Also Examples
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.
1 |
f |
A function to apply to the collection. |
x |
A collection, such as a list, matrix, or dataframe. |
m |
Margin. 1 to call |
... |
Arguments passed to |
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).
Matrix or dataframe.
https://github.com/robertschnitman/afp, lapply
, do.call
, rbind
, cbind
, https://purrr.tidyverse.org/
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
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.