I have a list of dataframes, which I would like to perform a merge on. How can I pass each element of the list to the respective sqldf-statement? My code is as follows:
require(sqldf)
a <- data.frame(ID=c("a","b","c","d","a","a"),num=1:6,num2=2:7)
b <- data.frame(ID=c("d","a","a","a","b","c"),num=6:1,num2=7:2)
Datalist <- list(a,b)
mergeto <- data.frame(ID=c("a","a","a","b","c","d"),name=rep("A",6),name2=rep("B"),6)
test <- sqldf("
select *
from Datalist[[1]] as a left outer join mergeto as b
where a.ID = b.ID and
")
I get the error:
Error in rsqlite_send_query(conn@ptr, statement) : unrecognized token: "]"
Is there any way to use the list variables directly from R? I mean I know I could use a <- Datalist[[1]]
and then use a
in the sqldf-statement, but this way a create a copy of Datalist elements every time and Datalist is already very large..I am sure there must be a way. Also to access variables directly that contain strings, there must be a way?
So what I want to do in the end is perform SQL-statements in a loop, so I must also be able to change the index within the statement. But because it's a string, it doesn't seem easily possible anymore.