I have a data frame with stock data (date, symbol, high, low, open, close, volume). Using r and mysql and sqldf and rmysql I have a list of unique dates and unique stock symbols.
What I need now is to loop through the data and find the close on two specified dates. For instance:
stkDatacontains (date, symbol, high, low, open, close, volume)datescontains unique datessymbolcontains unique symbols
I want to loop through the lists in a sqldf statement as such:
'select stkData$close from stkData where symbol = symbol[k] and date = dates[j]'
k and j would be looped numbers, but my problem is the symbol[k] and dates[j] parts.
sqldf won’t read them properly (or I can’t code properly). I’ve tried as.Date, as.character with no luck. I get the following error message:
Error in sqliteExecStatement(con, statement, bind.data) :
RS-DBI driver: (error in statement: near "[4,]": syntax error)
You’re pretty far off in terms of syntax for sqldf, unfortunately. You can’t use
$or[]notations in sqldf calls because those are both R syntax, not SQL syntax. It’s an entirely separate language. What’s happening is that sqldf is taking your data frame, importing it into SQLite3, executing the SQL query that you supply against the resulting table, and then importing the result set back into R as a data frame. No R functionality is available within the SQL.It’s not clear to me what you’re trying to do, but if you want to run multiple queries in a loop, you probably want to construct the SQL query as a string using the R function
paste(), so that when it gets to SQLite3 it’ll just be static values where you currently havesymbol[k]anddates[j].So, you’ll have something like the following, but wrapped in a loop for
jandk: