0

Problem: What is the best way to loop through a vector of IDs, so that one ID is passed as an argument to a function, the function runs, then the next ID is used to run the function again, and so on until the function has been run 30 times with the 30 IDs in my vector?

Additional Info: I have a complex function that retrieves data from several different sources, manipulates it, writes it to a different table, and then emails me when its done. It has several arguments that are hard coded in, and an ID argument that I manually input each time I want to run it.

I'm sorry that I can't give a lot of specifics, but this is an extremely simplified version of my setup

#Manually Entered Arguments

ID<-3402
Arg1<- "Jon_Doe"
Arg2<- "Jon_Doe@gmail.com"

#Run Function
  RunFun <- function (ID, arg1, arg2) {...}

Now, I have 30 non-sequential IDs (all numerical) that I have imported from an Excel column using:

ID.Group<- scan()

I know that it is extremely inefficient to run each ID through the function one at a time, but the complexity of the function and technological limitations only allow for one to be run at a time.

I am just getting started with R, so I'm sorry if any of this didn't make sense. I have spent the last 5 hours trying to figure this out so any help would be greatly appreciated. Thank you!

dsal1951
  • 1,630
  • 1
  • 16
  • 20
  • It would be easier for people to help you if you gave more details. Have a look at this: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – betabandido Dec 11 '13 at 23:21
  • you seem to be asking for a description of scalar vs vectorised functions... – Stephen Henderson Dec 11 '13 at 23:26

1 Answers1

0

The Vectorize function is actually a wrapper to mapply and is often used when vectorization is not a natural outcome of the function body. If you wrote the function with values for the arg1 and arg2 like this:

RunFun <- function (ID, arg1="Jon_Doe", arg2="Jon_Doe@gmail.com") {...}
V.RunFun <- Vectorize(Runfun)
V.RunFun ( IDvector )

This is often used with integrate or outer which require that their arguments return a vector of equal length to input.

IRTFM
  • 258,963
  • 21
  • 364
  • 487