How can I extract the month using sqldf package

青春壹個敷衍的年華 提交于 2019-12-02 14:05:22

问题


I tried to get a view that is based on group by of date by using sqldf package and a month function but I got an error :Error in sqliteSendQuery(con, statement, bind.data) : error in statement: no such function: month

Here is my query: s<-sqldf("select month(dateTime),sum(wolfs) group by dateTime")

Attached is a toy data frame:

 df <- read.table(text = "dateTime         birds    wolfs     snakes
                         2014-05-21        9         7    a
                         2014-04-28        8         4    b
                         2014-04-13        2         8    c
                         2014-03-12        2         3    a
                         2014-02-04        8         3    a
                         2014-02-29        1         2    a
                         2014-01-17        7         1    b
                         2014-01-16        1         5    c
                         2014-09-20        9         7    c
                         2014-08-21        8         7    c ",header = TRUE)

How can I extract the month using sqldf package?


回答1:


I suspect you are used to SQL Server, but the sqldf backend being used in your case is SQLite, where there is no MONTH function. Try this instead:

R> sqldf("SELECT strftime('%m', dateTime) AS Month
            ,SUM(wolfs) AS Wolves
         FROM df
         GROUP BY strftime('%m', dateTime)")
#    Month Wolves
#  1    01      6
#  2    02      5
#  3    03      3
#  4    04     12
#  5    05      7
#  6    08      7
#  7    09      7



来源:https://stackoverflow.com/questions/31407970/how-can-i-extract-the-month-using-sqldf-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!