mdply: Call function with arguments in array or data frame,...

Description Usage Arguments Details Value Input Output References See Also Examples

View source: R/mdply.r

Description

Call a multi-argument function with values taken from columns of an data frame or array, and combine results into a data frame

Usage

1
2
mdply(.data, .fun = NULL, ..., .expand = TRUE, .progress = "none",
  .inform = FALSE, .parallel = FALSE, .paropts = NULL)

Arguments

.data

matrix or data frame to use as source of arguments

.fun

function to apply to each piece

...

other arguments passed on to .fun

.expand

should output be 1d (expand = FALSE), with an element for each row; or nd (expand = TRUE), with a dimension for each variable.

.progress

name of the progress bar to use, see create_progress_bar

.inform

produce informative error messages? This is turned off by default because it substantially slows processing speed, but is very useful for debugging

.parallel

if TRUE, apply function in parallel, using parallel backend provided by foreach

.paropts

a list of additional options passed into the foreach function when parallel computation is enabled. This is important if (for example) your code relies on external data or packages: use the .export and .packages arguments to supply them so that all cluster nodes have the correct environment set up for computing.

Details

The m*ply functions are the plyr version of mapply, specialised according to the type of output they produce. These functions are just a convenient wrapper around a*ply with margins = 1 and .fun wrapped in splat.

Value

A data frame, as described in the output section.

Input

Call a multi-argument function with values taken from columns of an data frame or array

Output

The most unambiguous behaviour is achieved when .fun returns a data frame - in that case pieces will be combined with rbind.fill. If .fun returns an atomic vector of fixed length, it will be rbinded together and converted to a data frame. Any other values will result in an error.

If there are no results, then this function will return a data frame with zero rows and columns (data.frame()).

References

Hadley Wickham (2011). The Split-Apply-Combine Strategy for Data Analysis. Journal of Statistical Software, 40(1), 1-29. http://www.jstatsoft.org/v40/i01/.

See Also

Other data frame output: adply, ddply, ldply

Other multiple arguments input: m_ply, maply, mlply

Examples

1
2
3
4
mdply(data.frame(mean = 1:5, sd = 1:5), rnorm, n = 2)
mdply(expand.grid(mean = 1:5, sd = 1:5), rnorm, n = 2)
mdply(cbind(mean = 1:5, sd = 1:5), rnorm, n = 5)
mdply(cbind(mean = 1:5, sd = 1:5), as.data.frame(rnorm), n = 5)

Example output

  mean sd         V1         V2
1    1  1  0.0281318  0.9603721
2    2  2  3.6647118  5.7467023
3    3  3  6.4070505 -2.7127433
4    4  4  1.2098160  9.5733648
5    5  5 -5.9447450 -1.0400300
   mean sd         V1         V2
1     1  1  1.2506739  0.7594330
2     2  1  3.3733821  2.0537217
3     3  1  5.4556120  2.3165622
4     4  1  3.5802295  5.3253231
5     5  1  5.4384257  5.7115446
6     1  2  1.2419571 -2.0223169
7     2  2 -1.6976060  2.3125690
8     3  2  2.1137534  2.2454386
9     4  2  4.2050767  4.2625454
10    5  2  3.2627306  6.3577257
11    1  3 -3.6870592  7.8840932
12    2  3  5.6079898  1.5860156
13    3  3 -4.0223132 -0.1752903
14    4  3 -2.6084191  2.0463315
15    5  3  1.5435458  3.5188400
16    1  4 -2.4372223  0.5647384
17    2  4  4.8898557  2.1102315
18    3  4 -0.3103671  3.6353763
19    4  4  1.8073910  3.0327481
20    5  4  1.6112743  3.4861216
21    1  5  2.3765304 -5.1859103
22    2  5  9.6538316 -0.5331513
23    3  5 -4.2507880 -3.7359422
24    4  5  8.2971204  0.9058580
25    5  5  7.5059693 11.8869157
  mean sd        V1       V2       V3        V4        V5
1    1  1 2.3735868 0.458740 0.433311 0.4914755 0.1916439
2    2  2 0.5876147 6.308201 4.792018 0.9086048 2.3942631
3    3  3 0.8936273 9.725387 3.006424 3.3535177 8.3397970
4    4  4 2.7855436 3.689438 3.858531 6.2899890 3.6810345
5    5  5 4.4560882 1.327426 7.759834 8.1707367 5.6068403
   mean sd      value
1     1  1  2.5241037
2     1  1  1.1133229
3     1  1  1.2999567
4     1  1  1.6144720
5     1  1  0.1030883
6     2  2  1.3409487
7     2  2  1.6047732
8     2  2  1.9683570
9     2  2  5.2817933
10    2  2  2.4033436
11    3  3  9.1567933
12    3  3  1.9877205
13    3  3  7.3993067
14    3  3  3.1268524
15    3  3  5.2726129
16    4  4  5.7998091
17    4  4  0.1764000
18    4  4  2.0118731
19    4  4  4.7002937
20    4  4  5.6200545
21    5  5 -4.0449753
22    5  5  0.5471754
23    5  5  2.0633721
24    5  5  4.6482450
25    5  5  7.4577557

plyr documentation built on May 1, 2019, 9:23 p.m.