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
[email protected]
https://stat.ethz.ch/mailman/listinfo/r-help-es

Responder a