0

I have following code.

    for(i in seq(from=1, to=8, by=1)) #i is number of stocks from a list
    {
        for(j in seq(from=1, to=8, by=1)) #j is number of trading days from another list
        {
            ## Matching bid and ask prices of each stock for each date and update temp_table
            select_output <- sqldf("select * from temp_table where FLAG == 'new' ")
        }
    }

In current scenario only the last iteration data is staying in select_output which is expected. I want to keep the file open to populate it with data from all the iterations. I don't want to use another loop for inserting data into select_output. Is there any other way possible in R? Please suggest if possible.

Thanks in advance.

3 Answers 3

1

one option is to combine expand.grid, apply and do.call

index <- expand.grid(
  i = 1:8,
  j = 1:8
)
results <- apply(index, 1, function(x){
   x["i"] # holds i
   x["j"] # holds j
   sqldf("select * from temp_table where FLAG == 'new' ")
})
do.call(rbind, results)
Sign up to request clarification or add additional context in comments.

2 Comments

no I have more operations inside nested loops. So this way it will be not possible. I am taking data from other dataframes based on i and j positions. I am hoping - if keeping my nested loop intact, you can suggest something on appending data in select_output.
Why can't you do those other operations inside the function within apply, using x["i"] and x["j']?
0

I think this is actually an R weak point. The best thing to do is probably:

results <- data.frame(a=numeric(0), b=character(0), ...) # whatever columns you expect
for(i in 1:8) for(j in s1:7) {
        ## Your code...
        select_output <- sqldf("select * from temp_table where FLAG == 'new' ")
        results <- rbind(results, select_output)      
}

The problem here is that you need in advance to specify the format of results. This can be a good thing - it makes you state explicitly what you expect, and will give an error if those expectations are violated. But it would be nice to have a quick way to bind together a data frame. Maybe the Hadleyverse has a solution?

1 Comment

growing objects with rbind() or c() are usually a bad idea
0

Here is another solution.

results <- lapply(1:8, function(i){
  #do some stuff with i
  output <- lapply(1:8, function(j){
     #do some stuff with j based on the stuff with i
  })
  do.call(rbind, output)
})
do.call(rbind, results)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.