[R] SQLDF - Submitting Queries with R Objects as Columns

From: Mike Schumacher <mike.schumacher_at_gmail.com>
Date: Wed, 09 Mar 2011 08:41:56 -0800


Fellow R programmers,

I'd like to submit SQLDF statements with R objects as column names.

For example, I want to assign "X" to "var1" (var1<-"X") and then refer to "var1" in the SQLDF statement. SQLDF needs to understand that when I reference "var1", it should look for "X" in the dataframe.

This is necessary because my SQLDF is part of a larger function that I call that repeatedly with different column names.

Code below... thank you in advance!

Mike

library(sqldf)

testdf<-data.frame(c(1,2,3,4,5,6,7,8,9,10),c(1,1,1,2,2,2,3,3,3,3)) names(testdf)<-c("X","Y")

# Works as intended

sqldf("select sum(X) as XSUM,

       Y             as Y
       from testdf
       group by Y")

# Now... can I reference var1 in the code?
var1<-"X"

# Unsuccessful Atteps

sqldf("select sum(var1) as XSUM,

       Y             as Y
       from testdf
       group by Y")

sqldf("select sum(get(var1)) as XSUM,
       Y             as Y
       from testdf
       group by Y")

sqldf("select sum(return(var1)) as XSUM,
       Y             as Y
       from testdf
       group by Y")



-- 
Michael Schumacher
mike.schumacher_at_gmail.com
Manager Data & Analytics, ValueClick
818-851-8638

	[[alternative HTML version deleted]]

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 09 Mar 2011 - 21:48:34 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Mar 2011 - 22:30:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive