1

I currently am running a loop to check if Certain ID's are contained in a function being called from SQL, but the function table doesn't have the unique ID column. I want to attach the unique ID to the output, before I rbind the lists together.
My code currently looks like this:

channel = RODBC::odbcDriverConnect("some connection")
output <- list()

for (i in 1:nrow(df)){
unique_id <- df$col1[i]
query = sprintf("select * from table1('%s')",unique_id)

query = RODBC::sqlQuery(channel, query)
output[[i]] <- query
}

final_output <- do.call('rbind',output)
srty
  • 130
  • 7
  • `output[[i]] <- list(query = query, id = i)`? Btw, you should have that inside your loop, I guess. – Frank Oct 11 '18 at 19:42
  • (After that edit...) If query is a table, how about `query$ID <- i`? Or maybe `output[[i]] <- cbind(ID = i, query)` if you want it as first column. – Frank Oct 11 '18 at 20:09
  • 1
    @Frank Hey thanks a lot for the reply, I appreciate it. The way you recommended works. – srty Oct 11 '18 at 20:48
  • Ok cool, there are other ways as well, covered in the link, like adding an id afterwards with `data.table::rbindlist(output, idcol="ID")` – Frank Oct 11 '18 at 21:00

0 Answers0