1

If a function takes a data frame as one of it's arguments, is it possible to vectorize it? I have a custom function which takes the following arguments:

a.function<- function(a=c(),
                      b=data.frame(),
                      c=data.frame(),
                      d="",
                      e="",
                      f=data.frame()) {
  ...
}

Is there a data structure I could use which would allow me to use one of the *apply functions, so that I could run the function on many variables at once?


EDIT: Here's an example of how I'm currently running the code:

a <- c(1000,2000,1000)
b <- data.frame(type=c('string1',
                       'string2',
                       'string3'),
                value=c(2500,4000,3500),
                difference=c(0,30,0))
c <- data.frame(pd=4,
                gu=100)
d <- 'string4'
e <- 8

test <- a.function(a, b, c, d, e)
# test is a 1x3 character matrix
> test
[1] "44537" "0.1"   "B"

Together, a, b, c, d, and e describe a single group, and I run a.function on that group.. I would like to be able to define numerous such groups and then run a.function on all those groups at once. I realize I may need to significantly refactor the code; that's fine. Thanks!

5
  • 1
    The answer is yes. The particulars depend on you clarifying what exactly you want to vectorize. Please post an example, as there's no way to answer this in its current form. Commented Feb 17, 2012 at 14:17
  • @gsk3 - Thanks, I updated the post with a generic example. I'm somewhat limited in what I can post, unfortunately, as the model is proprietary. Please let me know if you need more detail. Commented Feb 17, 2012 at 14:47
  • 1
    @eykanal: I think what @gsk3 wants to see is which dataframes you want to *apply over, so replace the proprietary functions with, e.g., sin or paste . Commented Feb 17, 2012 at 15:35
  • I tried to explain that; in my example, a b c d and e are the different data frames (and other data types), and I want to apply a.function to them. I'm looking for a data structures solution. Joran mentioned lists below, I'll try that; if there are other methods of vectorizing, please let me know. Commented Feb 17, 2012 at 15:51
  • @eykanal We can't understand your motivation, please show more placeholder example code to give us an idea? Is it just cleaner syntax, or how does a.function combine its args a,b,c,d,e? How do they 'together describe a single group'? Please just make something basic up to illustrate your motivation. Commented Apr 12, 2014 at 23:50

2 Answers 2

2

what about a list of lists of data.frames...

my.list <- list(list1=list(a,b,c,d,e),list2=list(a2,b2,c2,d2,e2)... etc.)

then the plyr family of functions is where I would look.

llply(my.list,a.function)

they're handy for their easy parallelization, but its getting easy to use multiple cores with the apply family too (e.g. mclapply(my.list,a.function,...)). You would have to add a little front matter to your function to get the various data.frames

Sign up to request clarification or add additional context in comments.

Comments

1

What comes to mind first is to simply use mapply. You'd have a list associated with each argument to a.function: aList, bList, etc.

Each collection of ith elements of these lists would be arguments to successive calls to a.function. The call would look something like:

mapply(a.function,aList,bList,cList,dList,eList,SIMPLIFY = FALSE)

I include simplify = FALSE simply because I don't know what exactly you want the output to look like.

If functional programming is more your cup of tea, you can accomplish the same thing using ?Map.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.