[R] SQLDF from Variable Matrix

Gabor Grothendieck ggrothendieck at gmail.com
Wed Aug 4 12:22:48 CEST 2010


On Wed, Aug 4, 2010 at 12:29 AM, Suphajak Ngamlak
<Suphajak at phatrasecurities.com> wrote:
> Dear all,
>
> I would like to do sample statistics, e.g. mean, median from very large
> dataset. This is part of commands I use routinely with several dataset
> so I would like to make it into function. The simplified examples are
>
> Test<-data.frame(A=c('a','b','c','a','b','c'),B=c(1,2,3,4,5,6))
>
> #Create function (This one work)
>
> GetAvg<-function(Input,Bygroup){
> AVG<-fn$sqldf("select A, avg(B) as Average, median(B) as Median
>                from Test
>    group by $Bygroup")
> return(AVG)
> }
>
> Result<-GetAvg(Test,'A')
>
> #Create function (This one does not work)
>
> GetAvg<-function(Input,Bygroup){
> AVG<-fn$sqldf("select A, avg(B) as Average, median(B) as Median
>                from $Input
>    group by $Bygroup")
> return(AVG)
> }
>
> Result<-GetAvg(Test,'A')

That should be GetAvg('Test', 'A') with quotes around Test or if you
want to be able to specify the data unquoted then insert this line at
the top of your function:

Input <- deparse(substitute(Input))



More information about the R-help mailing list