【发布时间】:2015-08-07 13:28:56
【问题描述】:
我有一个数据,其中包含过去 3 年中每 5 分钟的指数报价(S&P500、CAC40、...),这使得它非常庞大。我正在尝试创建包含每次索引性能的新列(即([TIME] 的报价/昨天收盘时的报价)-1)和每个索引。我是这样开始的(我的数据名为 temp):
listIndexes<-list("CAC","SP","MIB") # there are a lot more
listTime<-list(900,905,910,...1735) # every 5 minutes
for (j in 1:length(listTime)){
Time<-listTime[j]
for (i in 1:length(listIndexes)) {
Index<-listIndexes[i]
temp[[paste0(Index,"perf",Time)]]<-temp[[paste0(Index,Time)]]/temp[[paste0(Index,"close")]]-1
# other stuff to do but with the same concept
}
}
但它很长。有没有办法摆脱 for 循环或更快地创建这些变量?我读了一些关于应用函数及其派生的东西,但我不知道是否应该以及如何在这里使用它。
我的数据如下所示:
date CACcloseyesterday CAC1000 CAC1005 ... CACclose ... SP1000 ... SPclose
20140105 3999 4000 40001.2 4005 .... 2000 .... 2003
20140106 4005 4004 40003.5 4002 .... 2005 .... 2002
...
我想要的输出将是一个新列(更确切地说是每次和每个索引的一个新列),它将被添加到 temp
date CACperf1000 CACperf1005... SPperf1000...
20140106 (4004/4005)-1 (4003.5/4005)-1 .... (2005/2003)-1 # the close used is the one of the day before
idem for the following day
我写 (4004/4005)-1 只是为了显示计算结果,但结果应该是一个数字:-0.0002496879
【问题讨论】:
-
如果没有看到您的数据是什么样子以及您想要的输出是什么,就很难回答。你能提供这些吗?
-
你能显示几行
temp -
数据是
xts对象吗? -
如果有帮助,我从 .csv 文件中导入了数据