sqldf and functions

307 views
Skip to first unread message

Stephen Mandel

unread,
Jan 21, 2014, 9:47:32 AM1/21/14
to sq...@googlegroups.com
I am working with a sql pull that looks like this

data<-sqldf("select date, a, b, c where c=980 and
b=1", row.names=TRUE)

Is it possible to create a function where the c and b values are passed to the expression?

TIA

Gabor Grothendieck

unread,
Jan 21, 2014, 10:05:48 AM1/21/14
to sq...@googlegroups.com
Preface sqldf with fn$ as in Example 5 on the home page
Also see ?fn

Here, we would have:

b <- 1
c <- 980
data <- fn$sqldf("select date, a, b, c where c=$c and  
                     b=$b", row.names=TRUE)

eastand...@gmail.com

unread,
Nov 10, 2015, 5:07:11 PM11/10/15
to sqldf
Gabor,
I haven't found a way to use fn$sqldf with something like this:
inradio<-function(x){head(fn$sqldf("select * from radiomarket where market like '%$x%'"))}

What is the syntax for a partial match instead of equals?

Gabor Grothendieck

unread,
Nov 10, 2015, 5:20:11 PM11/10/15
to sqldf

On Tuesday, November 10, 2015 at 5:07:11 PM UTC-5, Catherine Smith wrote:

Gabor, 
I haven't found a way to use fn$sqldf with something like this: 
inradio<-function(x){head(fn$sqldf("select * from radiomarket where market like '%$x%'"))} 

What is the syntax for a partial match instead of equals? 

 The example shown is missing the input, radiomarket.  If I supply my own it works.


> library(sqldf)
> radiomarket <- data.frame(market = c("abc", "def"))
> nradio<-function(x){head(fn$sqldf("select * from radiomarket where market like '%$x%'"))} 

> nradio("b")
  market
1    abc

> nradio("X")
[1] market
<0 rows> (or 0-length row.names)

Reply all
Reply to author
Forward
0 new messages