eykanal
eykanal

Reputation: 27017

Vectorizing an R function that takes data frames

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!

Upvotes: 1

Views: 147

Answers (2)

joran
joran

Reputation: 173577

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.

Upvotes: 1

Justin
Justin

Reputation: 43255

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

Upvotes: 2

Related Questions