-1

I have the following code, and I would like to name the dataframe created from sqldf, as t2_value_of_i. I then want to append those files into one single file, what is the recommended way of going about this? The iteration below overwrites as t2_i throughout the loop. Thank you.

m4<-data.frame(m3[-1,])

colnames(m4)<-c("iteration_criteria")
m4$iteration_criteria<-as.character(m4$iteration_criteria)


m4.list <-vector("list", nrow(m4))


for (i in 1:nrow(m4)) {
  m4.list[[i]] <- m4[i,] }

for (i in m4.list){


t2_i<-sqldf(print(paste(
"select a.*, b.iteration_criteria
 from data_by_zone_final a left join t1 b  on
                  (a.state_long=b.state_long) and 
                  (a.state_short=b.state_short) and 
                  (a.state_subregion_name=b.state_subregion_name)
where (a.local_hour_final=17) and b.iteration_criteria='",m4.list,"'",sep="" )     ))
}
SabDeM
  • 7,050
  • 2
  • 25
  • 38
tcc
  • 11
  • 1
  • 4
  • `t2_i = rbind(t2_i, sqldf(...)` ? what is the dimensions and type of your objects? – mts Aug 12 '15 at 18:23
  • 1
    The way to do this is with assign, but you should know that this probably isn't the best way to go about solving the problem – MichaelChirico Aug 12 '15 at 18:23
  • 1
    Don't use data frames named `m1`, `m2`, `m3`, ..., [use a list of data frames instead](http://stackoverflow.com/a/24376207/903061). I see you're already using a list somehow, but use it all the way! – Gregor Thomas Aug 12 '15 at 18:45

2 Answers2

0

You can use paste or paste0 combined with assign to update the variable name, as following:

final = c()
for (i in m4.list){
  temp <- assign(paste0('t2_', i), sqldf(print(paste(
    "select a.*, b.iteration_criteria
     from data_by_zone_final a left join t1 b  on
                      (a.state_long=b.state_long) and 
                      (a.state_short=b.state_short) and 
                      (a.state_subregion_name=b.state_subregion_name)
    where (a.local_hour_final=17) and b.iteration_criteria='",m4.list,"'",sep="" )     
  )))
  final <- rbind(final, temp)
}
Paulo MiraMor
  • 1,582
  • 12
  • 30
  • Thanks, is doing what I need it to do, however, I need to append each of these dataframes, do you have any recommendations? – tcc Aug 12 '15 at 19:11
  • Sorry, I didn't notice that when posting the answer. Do you need the intermediate results? If not, the answer from @mts is more appropriate. – Paulo MiraMor Aug 12 '15 at 19:20
  • I do need the intermediate results, after I have conducted the sqldf join, I plan to run to results through the following regression t2_i_prediction_temp<-cv.lm(df = t2_i, form.lm = formula(observed_zone_data ~ observed_local_hourly_temp), m=4, dots = FALSE, seed=30, plotit=TRUE, printit=TRUE) – tcc Aug 12 '15 at 19:26
  • I edited the question to attend your needs. It was inspired in @mts's answer and should do the trick. It worked in a simulation that I ran. – Paulo MiraMor Aug 12 '15 at 19:41
0

If you don't need the intermediate results, you can rbind to your final object right away:

final = c()
for (i in m4.list){
  temp = sqldf(print(paste(
    "select a.*, b.iteration_criteria
    from data_by_zone_final a left join t1 b  on
    (a.state_long=b.state_long) and 
    (a.state_short=b.state_short) and 
    (a.state_subregion_name=b.state_subregion_name)
    where (a.local_hour_final=17) and b.iteration_criteria='", i,"'",sep="" )     ))
  final = rbind(final, temp)
}
mts
  • 2,160
  • 2
  • 24
  • 34
  • the dataframe "final" is not producing the expected results of having all of the iterations within that dataframe, do you have any recommendations? It only has the last literation of "temp" – tcc Aug 12 '15 at 19:09
  • @tcc I had copied your original query but I think you had to replace m4.list in the query with i, I just edited my answer, check that, I'm afraid I don't have data to reproduce your issue. – mts Aug 12 '15 at 19:11
  • Actually, this is right, my error was that I had m4.list in my where statement, and @Paulo MiraMor helped me identify it. Thank you to the two of you! I am new to R so this is a learning curve to me. Thank you again! – tcc Aug 12 '15 at 19:50