开发者

Matching dates in sqldf

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:

  • stkData contains (date, symbol, high, low, open, close, volume)
  • dates contains unique dates
  • symbol contains unique symbols

I want to loop through the lists in a sqldf statement as such:

'select stkData$close from stkData where symbol 开发者_如何学Go= 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 have symbol[k] and dates[j].

So, you'll have something like the following, but wrapped in a loop for j and k:

sqldf(paste('select close from stkData where symbol = ', symbol[k],
            ' and date = ', dates[j]))


You might need to construct the select statement as a string with paste before it gets passed to your SQL caller. Something like:

combo_kj <- expand.grid(ksym=symbol[1:k], jdates=dates[1:j])

SQLcalls <- paste('select close from stkData where symbol = ',
                   combo_kj$ksym,
                   ' and date = '
                   combo_kj$jdates,
                   sep="")

And then loop over SQLcalls with whatever code you are using.


Preface sqldf with fn$ as shown and then strings within backticks will be replaced by the result of running them in R and strings of the form $variable will be replaced by the contents of that variable (provided the variable name contains only word characters). Note that SQL requires that character constants be put in quotes so be sure to surround the backticks or $variable with quotes:

fn$sqldf("select close from stkData 
   where symbol = '`symbol[k]`' and 
         date = '`dates[j]`' ")

To use the $variable syntax try this:

mysymbol <- symbol[k]
mydate <- dates[j]
fn$sqldf("select close from stkData 
   where symbol = '$mysymbol' and 
         date = '$mydate' ")

Also see example 5 on the sqldf github page: https://github.com/ggrothendieck/sqldf

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜