Hola Carlos: Gracias por responder!
Ayer caí en esta página al buscar el problema con google, pero no conseguir entender y implementar lo que sugiere. Sin entender nada, lo que hice fue: ======================== > library(MASS) > library (sqldf) # Per SQL > data(Aids2, package="MASS") > SQL_PROVA = function (XDADES, XWHE) + { + XDADES_SQL = sqldf ("select * from $XDADES $XWHE") + return(XDADES_SQL) + } > DADES_SEL = SQL_PROVA (XDADES = birthwt, XWHE = "where age < '40'") Error in sqliteSendQuery(con, statement, bind.data) : error in statement: near "$XDADES": syntax error ======================== Evidentemente da error. Tampoco funcionaba si introducía \"age\" en el agumento del where: ======================== > DADES_SEL = SQL_PROVA (XDADES = birthwt, XWHE = "where \"age\" < '40'") Error in sqliteSendQuery(con, statement, bind.data) : error in statement: near "$XDADES": syntax error ======================== He buscado ejemplos con google. pero no he encontrado nada. Gracias y saludos. On Mon, 27 Jul 2015 12:45:50 +0200 Carlos Ortega <c...@qualityexcellence.es> wrote: > Hola, > > Mira la página de "sqldf" en GitHub, viene un ejemplo, entre otras muchas > cosas, que lo explica: > > https://github.com/ggrothendieck/sqldf#Example_5._Insert_Variables > > #------------------------------ > # 5 > minSL <- 7 > limit <- 3 > fn$sqldf('select * from iris where "Sepal.Length" > $minSL limit $limit') > #------------------------------ > > Saludos, > Carlos Ortega > www.qualityexcellence.es > > > 2015-07-27 12:00 GMT+02:00 Griera <gri...@yandex.com>: > > > Hola: > > > > No consigo que la función sqldf () funcione dentro de una función. Alguien > > puede echarme una mano. En resumen, el problema es que cuando lo ejecuto > > fuera de una función no tengo ningún problema: > > > > ========================== > > > # install.packages("sqldf") > > > library(MASS) > > > library (sqldf) > > > data(Aids2, package="MASS") > > > options(digits=3) > > > > > table (Aids2$state) > > NSW Other QLD VIC > > 1780 249 226 588 > > > > > max (Aids2$age) > > [1] 82 > > > > > # Sin función: > > > DADES_SEL = sqldf ("select * from Aids2 where state in ('NSW', 'QLD') > > and age < 40") > > > > > table (DADES_SEL$state) > > NSW Other QLD VIC > > 1102 0 128 0 > > > > > max (DADES_SEL$age) > > [1] 39 > > ========================== > > > > Pero dentro de una función no se como pasarle los argumentos para que > > funcione: > > > > ========================== > > > SQL_PROVA = function (XDADES, XWHE) > > + { > > + XDADES_SQL = sqldf (paste ("select * from", XDADES, XWHE, sep = " ")) > > + return(XDADES_SQL) > > + } > > > DADES_SEL = SQL_PROVA (XDADES = birthwt, XWHE = "where age < '40'") > > Error in sqliteSendQuery(con, statement, bind.data) : > > error in statement: near "fromc": syntax error > > ========================== > > > > Alguien puede ayudarme y decir que estoy haciendo mal? El problema debe > > estar en el "paste" ya que si coloco la orden original funciona: > > > > ========================== > > > SQL_PROVA = function (XDADES, XWHE) > > + { > > + XDADES_SQL2 = sqldf ("select * from Aids2 where state in ('NSW', > > 'QLD') and age < 40") > > + return(XDADES_SQL2) > > + } > > > > > DADES_SEL2 = SQL_PROVA (XDADES = birthwt, XWHE = "where age < '40'") > > > > > table (DADES_SEL2$state) > > NSW Other QLD VIC > > 1102 0 128 0 > > > > > max (DADES_SEL2$age) > > [1] 39 > > ========================== > > > > Muchas gracias y saludos. > > > > _______________________________________________ > > R-help-es mailing list > > R-help-es@r-project.org > > https://stat.ethz.ch/mailman/listinfo/r-help-es > > > > > > -- > Saludos, > Carlos Ortega > www.qualityexcellence.es _______________________________________________ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es