R 中 xts 对象中昨天的返回值

发布于 2024-10-18 22:10:14 字数 1107 浏览 5 评论 0原文

这是 xts 对象的尾部:

           SPY.Close    mavg     dn.1     up.1
2011-02-16    133.85 132.446 128.8502 130.9545
2011-02-17    134.25 132.793 129.0212 131.2241
2011-02-18    134.53 133.131 129.2198 131.5016
2011-02-22    131.83 133.117 129.4104 131.6236
2011-02-23    131.02 132.962 129.5961 131.7072
2011-02-24    130.93 132.828 129.7575 131.7792

给定一个每天传递的简单嵌套 ifelse() 函数:

signal <- ifelse(t$mavg > t$up.1, 1, ifelse(t$mavg < t$dn.1, -1, 99))

该规则中的值可以添加到该对象中:

t$signal = signal

生成新对象(我用一节来进行说明):

           SPY.Close    mavg      dn.1      up.1 signal
2010-11-18    119.96 120.713 118.17955 119.99845      1
2010-11-19    120.29 120.470 118.33112 120.09688      1
2010-11-22    120.19 120.240 118.47911 120.18489      1
2010-11-23    118.45 119.924 118.55112 120.20888     99
2010-11-24    120.20 119.734 118.63565 120.27635     99

如何我可以重写嵌套的 ifelse() 语句,以便每次计算结果为 99 时,都返回前一天的值吗?

注意:如果由于先有鸡还是先有蛋的悖论而无法按照指定的方式编写嵌套的 ifelse() 语句,则使用单独的语句将 99 变为 1 就足够了。

This is the tail of an xts object:

           SPY.Close    mavg     dn.1     up.1
2011-02-16    133.85 132.446 128.8502 130.9545
2011-02-17    134.25 132.793 129.0212 131.2241
2011-02-18    134.53 133.131 129.2198 131.5016
2011-02-22    131.83 133.117 129.4104 131.6236
2011-02-23    131.02 132.962 129.5961 131.7072
2011-02-24    130.93 132.828 129.7575 131.7792

Given a simple nested ifelse() function passed across each day:

signal <- ifelse(t$mavg > t$up.1, 1, ifelse(t$mavg < t$dn.1, -1, 99))

The value from this rule can be added to the object:

t$signal = signal

Yielding the new object (I've taken a section for illustration):

           SPY.Close    mavg      dn.1      up.1 signal
2010-11-18    119.96 120.713 118.17955 119.99845      1
2010-11-19    120.29 120.470 118.33112 120.09688      1
2010-11-22    120.19 120.240 118.47911 120.18489      1
2010-11-23    118.45 119.924 118.55112 120.20888     99
2010-11-24    120.20 119.734 118.63565 120.27635     99

How can I re-write the nested ifelse() statement so that each time it evaluates to 99, the value from the previous day is returned instead?

NOTE: if the nested ifelse() statement cannot be written as specified due to a chicken/egg paradox, then a separate statement to turn the 99 into 1 will suffice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄荷港 2024-10-25 22:10:14

您放弃 99 约定并使用真正的 NA。那么您应该熟悉zoo::na.locf,它的意思是“对于NA's,最后的观察结转”。

M <- read.table(textConnection(" SPY.Close    mavg      dn.1      up.1
2010-11-18    119.96 120.713 118.17955 119.99845   
2010-11-19    120.29 120.470 118.33112 120.09688   
2010-11-22    120.19 120.240 118.47911 120.18489   
2010-11-23    118.45 119.924 118.55112 120.20888   
2010-11-24    120.20 119.734 118.63565 120.27635   
") )
M <- cbind(M, signal = ifelse(M$mavg > M$up.1, 1, ifelse(M$mavg < M$dn.1, -1, NA)) )
M$signal <- na.locf(M$signal)

You drop the 99 convention and use real NA's. Then you should make the acquaintance of zoo::na.locf, which reads "for NA's , last observation carry forward".

M <- read.table(textConnection(" SPY.Close    mavg      dn.1      up.1
2010-11-18    119.96 120.713 118.17955 119.99845   
2010-11-19    120.29 120.470 118.33112 120.09688   
2010-11-22    120.19 120.240 118.47911 120.18489   
2010-11-23    118.45 119.924 118.55112 120.20888   
2010-11-24    120.20 119.734 118.63565 120.27635   
") )
M <- cbind(M, signal = ifelse(M$mavg > M$up.1, 1, ifelse(M$mavg < M$dn.1, -1, NA)) )
M$signal <- na.locf(M$signal)
狼性发作 2024-10-25 22:10:14

您想要前一天的哪个值?如果您想要前一天的signal,则在ifelse 完成生成之前无法访问昨天的值。试试这个:

tick$lagsignal <- c(NA, head(tick$signal, -1))
tick$newsignal <- ifelse(tick$signal == 99, tick$lagsignal, tick$signal)

Which value from the previous day do you want? If you want signal from the previous day, you can't access yesterday's value before ifelse finishes producing it. Try this instead:

tick$lagsignal <- c(NA, head(tick$signal, -1))
tick$newsignal <- ifelse(tick$signal == 99, tick$lagsignal, tick$signal)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文