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.schumac...@gmail.com
Manager Data & Analytics, ValueClick
818-851-8638

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to