目前嘗試寫一些會從一個有序列表小於日期X.搜索列表基於最接近的日期
現在我有這個返回的最後日期:它得到天的列表,並在我們將要進行搜索的那一天獲取索引,並查看我們想要返回的日期範圍。
之後它檢查日期是否存在(例如2月30日)。如果日期不存在,它會將日期減少1,然後再次應用篩選器(否則它會嘗試從NA
減去1天並失敗)。
library(lubridate)
getDate <- function(dates,day,range){
if(range == 'single')
{return (day-1)}
z <- switch(range,
single = days(1),
month = days(30),
month3 = months(3),
month6 = months(6),
year = years(1)
)
new_day <-(dates[day]-z)
i <- 1
while (is.na(new_day)){
new_day <- dates[day] - days(i) - z
}
ind<-which.min(abs (diff <-(new_day-dates)))
if (diff[ind] < 0)
{ind <- ind -1}
return (ind[1])
}
雖然此功能起作用,但問題在於速度效率。我有一種感覺,which.min(abs())
是最快的,我想知道是否有更好的選擇(除了寫我自己的功能搜索列表之外)。
stocks <- list(structure(list(sec = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L), mday = c(2L, 3L, 4L, 7L, 8L, 9L, 10L, 11L, 14L, 15L, 16L, 17L,
18L, 22L, 23L, 24L, 25L, 28L, 29L, 30L, 31L, 1L, 4L, 5L, 6L), mon = c(0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
1L, 1L, 1L), year = c(108L, 108L, 108L, 108L, 108L, 108L, 108L, 108L, 108L,
108L, 108L, 108L, 108L, 108L, 108L, 108L, 108L, 108L, 108L, 108L, 108L, 108L,
108L, 108L, 108L), wday = c(3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L), yday = c(1L, 2L, 3L, 6L, 7L,
8L, 9L, 10L, 13L, 14L, 15L, 16L, 17L, 21L, 22L, 23L, 24L, 27L, 28L, 29L, 30L,
31L, 34L, 35L, 36L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("sec", "min",
"hour", "mday", "mon", "year", "wday", "yday", "isdst"), tzone = "UTC",
class = c("POSIXlt", "POSIXt")))
old_pos <- getDate(stocks[[1]],21,"month") #should return 0
old_pos <- getDate(stocks[[1]],22,"month") #should return 1
這不返回矢量,也不是迄今爲止,只有一個指標,主要問題不在於工作(其中它),但它的優化。
該值稍後在另一個函數中使用,一種可能的加速是首先將所有舊索引匹配到新索引,然後將其作爲另一個列表返回。但不知道它是否會提供任何加速。
請提供用於測試的一些可再生的數據。 – Roland
http://pastebin.com/sDXMSft6或 http:// pastebin。com/vLVvwjHd (唯一的值是以股票形式傳入股票[[1]]) –
而不是粘貼到打印數據的鏈接,請提供'dput(head(stocks [[1]],20 ))和我們應該如何調用getDate函數的例子。那麼你的問題將包含一個最小的[可重現的例子](http://stackoverflow.com/q/5963269/271616)。 –