Hi! I have 2 list objects One list contains the data (list1) the second list2 contains the e.g linear model for each element of list 1. list2 <- lapply(list1,mylm,response) I obtain list 2 by something like this. now I have a function myfunc which takes an element from list1 and the coresponding element from list2 does something and .... list3[[x]] <- myfunc(list1[[x]],list2[[x]]) Until now I am runnign this in a for(x in 1:length(list1)) loop. But "for" loops are slow especially if list3 is HUGE. What can I do? Sincerely Eryk
Peter Dalgaard
2004-Jun-02 14:13 UTC
[R] How to iterate through two objects of class "list" fast?
"Wolski" <wolski at molgen.mpg.de> writes:> list3[[x]] <- myfunc(list1[[x]],list2[[x]]) > > Until now I am runnign this in a for(x in 1:length(list1)) loop. But "for" loops are slow especially if list3 is HUGE. > > What can I do?Use mapply(). I wouldn't expect the for loop to be all that slow unless you have forgotten to allocate memory for the result list up front, though. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907