I have a function with the follow signare: apply.strategy(instr, strat) where instr and strat are both objects of classes instrument and strategy respectively. I want to apply this function to a list that holds objects of the class instrument. Currently I am doing this by explicit looping: for(i in length(instr.list) ) { apply.strategy(instr.list[[i]], my.strat) } Is it possible to achieve this task by using one of the built in functions in R? I have looked at lapply but can't quite figure out how to pass the arguments. What would be the performance implications of one method vs. the other? Thanks, Kushan [[alternative HTML version deleted]]
On reply to the post http://r.789695.n4.nabble.com/using-lapply-td3345268.html Dear Kushan, this may be a good start: ## assuming 'instr.list' is your list object and you are applying my.strat() function on each element of that list, you can use lapply function as lapply(instr.list, function(x) return(my.strat(x))) Here resulting element will again be another list with length is same as the length of your original list 'instr.list.' Instead if the returned object for my.strat() function is a single number then you might want to create a vector instead list, in that case just use 'sapply' HTH Arun, [[alternative HTML version deleted]]
On 10.03.2011 03:46, Kushan Thakkar wrote:> I have a function with the follow signare: > > apply.strategy(instr, strat) > > where instr and strat are both objects of classes instrument and strategy > respectively. > > I want to apply this function to a list that holds objects of the class > instrument. > > Currently I am doing this by explicit looping: > > for(i in length(instr.list) ) { > apply.strategy(instr.list[[i]], my.strat) > }lapply(instr.list, apply.strategy, strat=mystrat) Uwe Ligges> > Is it possible to achieve this task by using one of the built in functions > in R? I have looked at lapply but can't quite figure out how to pass the > arguments. > > What would be the performance implications of one method vs. the other? > > Thanks, > Kushan > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.