从R
调用KDB服务器上的函数嗨,我在KDB服务器上写了一个函数,它给出了每个日期的输出列表。我必须计算日期集的函数,但是当我将它作为向量传递时,日期格式不起作用。请看下面的代码:
per <- c('2014.07.31', '2014.07.30','2014.07.29','2014.09.28')
per <- as.Date(per,"%Y.%m.%d")
PWPfunc <- function(k){
execute(h,"pwp_frac[0.05;0.1;k;`AAPL]")
}
output <- lapply(per,PWPfunc).
KDB无法识别R的日期格式。请帮忙。
答案 0 :(得分:1)
我通常会生成一个日期列表,并使用sub
函数
dates <- seq(as.Date("2014/11/01"), as.Date("2014/12/01"), by = "day")
dates <- sub('-',".",dates);dates <- sub('-',".",dates);
dates
[1] "2014.11.01" "2014.11.02" "2014.11.03" "2014.11.04" "2014.11.05" "2014.11.06" "2014.11.07" "2014.11.08" "2014.11.09" "2014.11.10"
[11] "2014.11.11" "2014.11.12" "2014.11.13" "2014.11.14" "2014.11.15" "2014.11.16" "2014.11.17" "2014.11.18" "2014.11.19" "2014.11.20"
[21] "2014.11.21" "2014.11.22" "2014.11.23" "2014.11.24" "2014.11.25" "2014.11.26" "2014.11.27" "2014.11.28" "2014.11.29" "2014.11.30"
[31] "2014.12.01"
如果您想从列表中删除周末,可以使用 chron 库:
require(chron)
dates <- seq(as.Date("2014/11/01"), as.Date("2014/12/01"), by = "day")
dates <- dates[!is.weekend(dates)]
dates <- sub('-',".",dates);dates <- sub('-',".",dates);
dates
[1] "2014.11.03" "2014.11.04" "2014.11.05" "2014.11.06" "2014.11.07" "2014.11.10" "2014.11.11" "2014.11.12" "2014.11.13" "2014.11.14"
[11] "2014.11.17" "2014.11.18" "2014.11.19" "2014.11.20" "2014.11.21" "2014.11.24" "2014.11.25" "2014.11.26" "2014.11.27" "2014.11.28"
[21] "2014.12.01"