当我为datename或datepart运行sqldf包时,出现错误消息。 这是我用过的表。
Height Date
163 12/01/90
182 11/13/88
167 5/14/97
172 3/18/94
170 10/11/92
185 7/15/90
expected_table <-sqldf("select
[Height],
(datename(weekday,[Date])) AS [Day of Week]
from table1
")
Error in sqliteSendQuery(con, statement, bind.data) :
error in statement: no such column: weekday
如果没有,是否有办法从[日期]获得工作日?
答案 0 :(得分:2)
sqldf是一个薄层,它将数据和SQL语句传递给您选择的后端。默认的后端是sqlite,但它也支持H2,PostgreSQL和MySQL。 SQLite不支持datename
,但H2支持dayname
:
# reproducibly set up input data frame
Lines <- "Height Date
163 12/01/1990
182 11/13/1988
167 5/14/1997
172 3/18/1994
170 10/11/1992
185 7/15/1990"
DF <- read.table(text = Lines, header = TRUE)
DF$Date <- as.Date(DF$Date, "%m/%d/%Y") # Date class
library(RH2) # if RH2 is loaded sqldf will automatically use H2 instead of sqlite
library(sqldf)
sqldf("select *, dayname(Date) Weekday from DF")
,并提供:
Height Date Weekday
1 163 1990-12-01 Saturday
2 182 1988-11-13 Sunday
3 167 1997-05-14 Wednesday
4 172 1994-03-18 Friday
5 170 1992-10-11 Sunday
6 185 1990-07-15 Sunday
注意:当然在直接R中也很容易做到这一点:
transform(DF, Weekday = format(Date, "%A"))
,并提供:
Height Date Weekday
1 163 1990-12-01 Saturday
2 182 1988-11-13 Sunday
3 167 1997-05-14 Wednesday
4 172 1994-03-18 Friday
5 170 1992-10-11 Sunday
6 185 1990-07-15 Sunday