Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] mdply equivalent #441

Closed
baptiste opened this issue May 26, 2014 · 3 comments
Closed

[FR] mdply equivalent #441

baptiste opened this issue May 26, 2014 · 3 comments
Assignees
Labels
feature a feature request or enhancement
Milestone

Comments

@baptiste
Copy link

It would be great if dplyr had a function similar to mapply, or better yet, mdply. It's just so useful, and clashes between dplyr and plyr mean that we can't use both.

Minimal example,

require(plyr)
p = expand.grid(mean=1:5, sd=seq(0, 1, length=10))
mdply(p, rnorm, n=5)
@hadley
Copy link
Member

hadley commented Jul 28, 2014

One approach:

p <- expand.grid(mean=1:5, sd=seq(0, 1, length=10))
p %>% do(data.frame(rnorm(.$mean, .$sd)))

Do you have more examples of using mdply() with data frames?

@baptiste
Copy link
Author

I use this pattern all the time, specifically to avoid this manual mapping inside rnorm: mdply does this argument-matching for me, automatically.

Here's a tutorial example for an external tool called from the command line (with various non-vectorised parameters) https://github.com/baptiste/adda/wiki/wrapper_primer (last section)

I have lots of other examples in my wiki pages
https://github.com/baptiste/planar/wiki/bragg_stack
https://github.com/baptiste/planar/wiki/kretschmann_reflectivity (section 2)
https://github.com/baptiste/cda/wiki/dimer_linear
etc.

They're all pretty specific to light-scattering simulations, but the pattern is very general:

  • define named grid of parameters
  • mdply matches the names in the function call for each row, and (optionally) fills in additional parameters (...)
  • the results are readily plotted with ggplot2, as already split by variables

@hadley hadley added this to the 0.3.1 milestone Aug 1, 2014
@hadley hadley self-assigned this Aug 1, 2014
@hadley hadley modified the milestones: 0.3.1, 0.4 Nov 18, 2014
@hadley
Copy link
Member

hadley commented Oct 22, 2015

This now feels more like a job for purrr.

@hadley hadley closed this as completed Oct 22, 2015
@lock lock bot locked as resolved and limited conversation to collaborators Jun 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants