Using lapply instead of a while loop

Hey there,

I don't know how to go on and need your help.

I want to get data with the following GetRemoteData-function and the problem are the changing day-ahead values. They should go from 1 to 10 and increase by one after every download. Every download should be an extra data.table and namen by the number of the days ahead. I tried doing this by using a while-loop, but at the end the full table will be overwritten. How can I do this by using apply()?

daysAhead <- 10
i <- 0


while(i  <= daysAhead) {
  
  
    dt.data <- GetRemoteData(source="EQ",v.ids = v.ids, 
                                fromDate = fromDateDP, toDate = toDateDP, 
                                l.eqPars = "days-ahead" = i)
i <- i+1
return(dt.data)
   
}

Thank you :slight_smile:

Hi,
The lapply / apply should look something like this

sapply(1:10, USE.NAMES = TRUE, simplify = FALSE, FUN = function(i) {
  GetRemoteData(source="EQ",v.ids = v.ids, 
                fromDate = fromDateDP, toDate = toDateDP, 
                l.eqPars = "days-ahead" = i)
})

I did it with sapply (see ?sapply)
PS: in your while loop you start at days-ahead 0 and not 1

Thank you so much!
This works perfectly :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.