R函数不返回日期期间的值

时间:2015-08-25 20:08:55

标签: r datetime

这是功能:

getData<-function(startDate, endDate, index, dateFrequency){
  hedge<-read.csv("f1.csv", header = TRUE)
  Date <- as.Date(hedge$Date, format='%m-%d-%Y')
  hedge <- cbind(Date, hedge[,-1])

  sD <- as.Date(startDate, "%Y-%m-%d")
  eD <- as.Date(endDate, "%Y-%m-%d")

  if (dateFrequency == "monthly"){
    query <- paste0('select HFRIEMNI from hedge where Date between "',sD,'"', ' and "', eD, '"')
    writeLines(query)
    d <- sqldf(query)
  }
  result <- d
  return(result)
}

这是f1.csv档案:

Date,HFRIEHI,HFRIEMNI
10-20-2000,-3.34%,1.23%
10-21-2000,2.85%,1.23%
10-22-2000,5.67%,0.82%
10-23-2000,-0.87%,0.73%
10-24-2000,5.92%,0.50%

使用此命令执行功能后:

getData("2000-10-20","2000-10-22", "HFRIEMNI","monthly")

我得到了0行,没有任何回报,但是我在此期间有值。

你能帮帮我吗,我做错了什么?我3年来无法解决这个问题。

1 个答案:

答案 0 :(得分:2)

尝试将查询更改为此行:

query <- paste0("select HFRIEMNI from hedge where Date between ", as.numeric(sD)," and ",as.numeric(eD) )