How can I pass R variable into sqldf?

Sandeep picture Sandeep · Sep 25, 2011 · Viewed 7.7k times · Source

I have some query like this:

sqldf("select TenScore from data where State_P = 'AndhraPradesh'")

But I have "AndhraPradesh" in a variable stateValue. How can I use this variable in a select query in R to get the same result as above.

Please show me the syntax.

Answer

rcs picture rcs · Sep 25, 2011

You can use sprintf:

sqldf(sprintf("select TenScore from data where State_P = '%s'", stateValue))