Vaibhav Thapliyal Vaibhav Thapliyal - 26 days ago 10
R Question

return list of dataframe from inside a function to apply rbind using do.call()

Below is a trimmed version of my script. I am hoping my function will return me a list per loop iteration, so that I can rbind all the list to form a new data frame, but when I am executing this script, I keep getting the error:

do.call("rbind", listofdfs) : object 'listofdfs' not found

Thank you all for your help.

library(DBI)
library(RPostgreSQL)

drv<- dbDriver("MyDataBase")
con<-dbConnect(drv,dbname="DB_Name",
host="DB_Location",port=number,user="MyName",password= "Password")


dates <- seq(as.Date(as.character(Sys.Date() - 33)), as.Date(as.character(Sys.Date() - 1)), by=1)


my_function<-function(dates){

listofdfs<-list()

for(i in 1:length(dates){

data<-dbGetQuery(con, sprintf("select X,Y,Z from TABLE where date>=date('%s')", dates[i])


data$newColumn<-mean(data$X)

listofdfs[[i]]<-data
}

return(listofdfs)

}

df<-do.call("rbind", listofdfs)


I have a small simplified example to refer, please refer to the dates variable from above

my_list_function<-function(dates){

for(i in 1:length(dates))
{

my_list<-list()

my_list[[i]]<-i

}
return(my_list) }

k<-do.call(rbind,my_list(dates))
View(k)


now running
do.call(rbind,my_list(dates))
returns error could not find function "my_list" and running do.call(rbind,my_list_function(dates)) works but is only giving 33.

Thanks again for help.

Answer

listofdfs is a variable that is declared within your function. Therefore it is not defined outside of its body.

but because it is returned by the function, you can access it by calling the function itself:

df<-do.call("rbind", my_function(dates))

Also on to make you small example work:

my_list_function<-function(dates){
  my_list<-list()
  for(i in 1:length(dates))
  {  
    my_list[[i]]<-i
  }
  return(my_list) 
}

k<-do.call(rbind,my_list_function(dates))