Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中RSQL Lite引擎中的自有函数_R_Rsqlite - Fatal编程技术网

R中RSQL Lite引擎中的自有函数

R中RSQL Lite引擎中的自有函数,r,rsqlite,R,Rsqlite,我找到了这个用于SAS的SQL代码,我想将其转换为RSQL Lite 出现的第一个问题是R没有提供intck函数,该函数返回两个日期之间的月差。我发现一个类似的函数(在stackoverflow中)如下所示: mob<-function (begin, end) { begin<-paste(substr(begin,1,6),"01",sep="") end<-paste(substr(end,1,6),"01",sep="") mob1<-as.per

我找到了这个用于SAS的SQL代码,我想将其转换为RSQL Lite



出现的第一个问题是R没有提供intck函数,该函数返回两个日期之间的月差。我发现一个类似的函数(在stackoverflow中)如下所示:

mob<-function (begin, end) {
  begin<-paste(substr(begin,1,6),"01",sep="")
  end<-paste(substr(end,1,6),"01",sep="")
  mob1<-as.period(interval(ymd(begin),ymd(end)))
  mob<-mob1@year*12+mob1@month
  mob
}
GVKEY,datadate,fyear,fyr,bkvlps,permno
14489,19980131,1997,1,4.0155,11081
14489,19990131,1998,1,1.8254,11081
14489,20000131,1999,1,2.0614,11081
14489,20010131,2000,1,2.1615,11081
14489,20020131,2001,1,1.804,11081
CRSP文件如下所示

permno,date,ret
11081,20000103,0.1
11081,20000104,0.2


您只能在SQLite中使用SQL函数(以及用C编写的函数)。不能使用R函数

library(sqldf)
try(detach("package:RH2"), silent = TRUE)  # detach RH2 if present

sqldf("select a.*, b.ret, b.date
          from Annual_File as a 
          left join CRSP as b 
          on a.permno = b.permno and 
             b.date + 2440588.5 between julianday(a.datadate + 2440588.5, '+3 months') and 
                                        julianday(a.datadate + 2440588.5, '+12 months')")
此外,SQLite对于日期处理不是很好,因为它没有日期和时间类型。SQLite提供的函数可以解决问题(请参见最后的注释),但我建议您改用H2数据库。它内置了
datediff
。请注意,根据需要,您可能需要将最后两个参数的顺序颠倒为
datediff

library(RH2)
library(sqldf)

# create test data frames

Lines1 <- "GVKEY,datadate,fyear,fyr,bkvlps,permno
14489,19980131,1997,1,4.0155,11081
14489,19990131,1998,1,1.8254,11081
14489,20000131,1999,1,2.0614,11081
14489,20010131,2000,1,2.1615,11081
14489,20020131,2001,1,1.804,11081"

Lines2 <- "permno,date,ret
11081,20000103,0.1
11081,20000104,0.2"

fmt <- "%Y%m%d"

Annual_File <- read.csv(text = Lines1)
Annual_File$datadate <- as.Date(as.character(Annual_File$datadate), format = fmt)

CRSP <- read.csv(text = Lines2)
CRSP$date <- as.Date(as.character(CRSP$date), format = fmt)

# run SQL statement using sqldf

sqldf("select a.*, b.ret, b.date, datediff('month', a.datadate, b.date) diff
          from Annual_File as a 
          left join CRSP as b 
          on a.permno = b.permno and 
             datediff('month', a.datadate, b.date) between 3 and 14")

我已经编辑了上面的代码。问题基本上是我如何在RSQL Select语句中调用自己编写的函数?没有人能帮我解决这个问题。我想在RSQL select语句中调用自己编写的函数。甚至有可能做到这一点吗?如果缺少
library
语句,则代码依赖于特定的文件路径,因此无法按原样运行,请不要将
install.packages
语句放入发布到so的代码中(除非注释掉)。
Error in sqliteSendQuery(con, statement, bind.data) : 
  error in statement: no such function: mob
library(RH2)
library(sqldf)

# create test data frames

Lines1 <- "GVKEY,datadate,fyear,fyr,bkvlps,permno
14489,19980131,1997,1,4.0155,11081
14489,19990131,1998,1,1.8254,11081
14489,20000131,1999,1,2.0614,11081
14489,20010131,2000,1,2.1615,11081
14489,20020131,2001,1,1.804,11081"

Lines2 <- "permno,date,ret
11081,20000103,0.1
11081,20000104,0.2"

fmt <- "%Y%m%d"

Annual_File <- read.csv(text = Lines1)
Annual_File$datadate <- as.Date(as.character(Annual_File$datadate), format = fmt)

CRSP <- read.csv(text = Lines2)
CRSP$date <- as.Date(as.character(CRSP$date), format = fmt)

# run SQL statement using sqldf

sqldf("select a.*, b.ret, b.date, datediff('month', a.datadate, b.date) diff
          from Annual_File as a 
          left join CRSP as b 
          on a.permno = b.permno and 
             datediff('month', a.datadate, b.date) between 3 and 14")
  GVKEY   datadate fyear fyr bkvlps permno ret       date diff
1 14489 1998-01-31  1997   1 4.0155  11081  NA       <NA>   NA
2 14489 1999-01-31  1998   1 1.8254  11081 0.1 2000-01-03   12
3 14489 1999-01-31  1998   1 1.8254  11081 0.2 2000-01-04   12
4 14489 2000-01-31  1999   1 2.0614  11081  NA       <NA>   NA
5 14489 2001-01-31  2000   1 2.1615  11081  NA       <NA>   NA
6 14489 2002-01-31  2001   1 1.8040  11081  NA       <NA>   NA
library(sqldf)
try(detach("package:RH2"), silent = TRUE)  # detach RH2 if present

sqldf("select a.*, b.ret, b.date
          from Annual_File as a 
          left join CRSP as b 
          on a.permno = b.permno and 
             b.date + 2440588.5 between julianday(a.datadate + 2440588.5, '+3 months') and 
                                        julianday(a.datadate + 2440588.5, '+12 months')")