Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8670409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:42:02+00:00 2026-06-12T18:42:02+00:00

I wrote this nifty function to apply a function for every combination of vectorized

  • 0

I wrote this nifty function to apply a function for every combination of vectorized arguments:

require(plyr)
require(ggplot2)


###eapply accepts a function and and a call to expand grid
###where columns created by expand.grid must correspond to arguments of fun
##each row created by expand.grid will be called by fun independently

###arguments
##fun either a function or a non-empty character string naming the function to be called.
###... vectors, factors, or a list containing thse

###value
###a data frame

##Details
##at this time, elements of ... must be at least partially named to match args of fun
##positional matching does not work

###from the ddply documentation page:
###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. 

eapply <- function(fun,...){
    if(!is.character(fun)) fun <- as.character(substitute(fun))
    adply(
        expand.grid(...),
        1,
        function(x,fun) do.call(fun,x),
        fun
    )
}


##example use:

m <- function(n,visit.cost){
    if(n*visit.cost < 250){
        c("total.cost"=n*visit.cost)
    }else{
        c("total.cost"=250 + (n*visit.cost-250)*.25)
    }

}


d <- eapply(m, n=1:30, visit.cost=c(40,60,80,100))


ggplot(d,aes(x=n,y=total.cost,color=as.factor(visit.cost),group=visit.cost)) + geom_line()

How can I rewrite the function such that the arguments passed to expand.grid need not be named:

d <- eapply(m, 1:30, c(40,60,80,100))

Alternatively, are there any existing functions that have similar functionality?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:42:03+00:00Added an answer on June 12, 2026 at 6:42 pm

    Not the most elegant but this works. Most importantly, it allows you to pass variables to expand.grid without naming them.

    eeyore <- function(fun, ...){
        if(!is.character(fun)) fun <- as.character(substitute(fun))
    
    f <- match.fun(fun)
    args <- as.list(substitute(list(...)))[-1]
    foo <- expand.grid(llply(args, eval))
    foo$F <- apply(foo, 1, function(x) { f(x[[1]], x[[2]])})
    foo
    }
    
    d <- eeyore(m, 1:30, c(40,60,80,100))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this function to make columns sortable. I want to rearrange divs based
I wrote this script to my live search: $(document).ready(function(){ $('input#search').keypress(function(){ $('ul#pacientes_hint').html(); var texto =
I wrote a function which does this functionality and I am not satisfied with
I wrote this function in javascript function CheckBeforeAddNew(btnId, gridSelected) { $(btnId).click(function () { for
I wrote this function to trace a certain spot in a topological tree. However
John Resig wrote a nifty Class function, swanky. I'm trying to figure out what
I wrote this very simple function for my current project called insidelabel() that let's
I wrote this code for zoom in/out . it works but even with one
I wrote this as a simple dice game. It works as I want except
I wrote this code for zoom in / out and it suppesed to only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.