如何向量化:根据上次二进制向量为 1 的时间设置一个值
我还有另一个 R 初学者问题...
我如何矢量化(避免 for 循环)以下代码:
# algorithm for getting entry prices (when signal > 0): look back from current
# position until you find first signal > 0,
# `mktdataclose` at that time is entry price
# `entryPrices` is an xts object representing entry prices
# if entryPrices are not available (is.null == TRUE) then wee need to reconstruct
# them from signal (xts object with 1 when entry signal triggered and 0
# otherwise) and close prices available in mktdataclose (an xts object with the
# same length as signal and same dates just that it represents closing prices)
EntryPrices <- entryPrices
if (is.null(EntryPrices)) {
# get entryprices as close prices on buy signal
EntryPrices <- ifelse(signal > 0, mktdataclose, 0)
entryPrice <- 0
for (i in 1:NROW(signal)) {
if (signal[i] > 0) entryPrice <- mktdataclose[i]
EntryPrices[i] <- entryPrice
}
}
我一直在思考 SAS 数据步骤方式,并绝望地寻找保留等。我在哪里可以找到一些简单的例子来理解 sapply等等(不幸的是,通过 ?sapply 提供的帮助对我来说太复杂了... :( )
谢谢您的帮助。
最好的, 萨摩。
I have another R beginner question...
How can I vectorize (avoid for loop in) following code:
# algorithm for getting entry prices (when signal > 0): look back from current
# position until you find first signal > 0,
# `mktdataclose` at that time is entry price
# `entryPrices` is an xts object representing entry prices
# if entryPrices are not available (is.null == TRUE) then wee need to reconstruct
# them from signal (xts object with 1 when entry signal triggered and 0
# otherwise) and close prices available in mktdataclose (an xts object with the
# same length as signal and same dates just that it represents closing prices)
EntryPrices <- entryPrices
if (is.null(EntryPrices)) {
# get entryprices as close prices on buy signal
EntryPrices <- ifelse(signal > 0, mktdataclose, 0)
entryPrice <- 0
for (i in 1:NROW(signal)) {
if (signal[i] > 0) entryPrice <- mktdataclose[i]
EntryPrices[i] <- entryPrice
}
}
I am stuck at thinking SAS data step way and desperatley looking for retain etc. Where can I find some simple exaples in order to understand sapply etc (r help via ?sapply is unfortunately to complicated for me... :( )
Thank you for your kind help.
Best,
Samo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解正确,你的问题是:你有两个长度为 n 的向量 signal 和 mktdataclose ,并且你想创建一个新向量 < code>EntryPrices 长度为
n
,使得mktdataclose[i]
是mktdataclose
最后 的值> 时间signal
在时间i
或之前为 1。您可以在没有 for 循环的情况下使用 cummax 来完成此操作,这是一个经常出乎意料的有用函数(请注意,这个问题与您之前的一些问题类似,使用此函数和类似地解决了这些问题>cumsum
)。在这里,我们使用 Gavin 的数据:我们的问题实际上是将信号向量转换为适当索引的向量:
这正是我们想要的索引,除了0. 现在,我们通过从
mktdataclose
中提取非零索引
处的值来设置EntryPrices
:If I understood correctly, your problem is: you have two vectors
signal
andmktdataclose
of lengthn
, and you want to create a new vectorEntryPrices
of lengthn
such thatmktdataclose[i]
is the value ofmktdataclose
the last timesignal
was 1 at or before timei
. You can do this without a for loop, usingcummax
, an often unexpectedly useful function (Note that this question is similar in flavor to some of your earlier questions, which were similarly solved using this function andcumsum
). Here we go, using Gavin's data:Our problem is really to convert the
signal
vector into a vector of the appropriate indices:This is exactly the
indices
we want, except for the 0. Now we setEntryPrices
by extracting the values at the non-zeroindices
frommktdataclose
:由于信号是 0 和 1 我想你可以用以下方法进行矢量化:
since signal is 0 and 1 i presume you can vectorize with:
您可能会发现另一种更直接的解决方案。我正在使用普拉萨德的伪数据。
Here's another solution that you may find more straight-forward. I'm using Prasad's psudo-data.