如何在不同周期之间为XTS绑定并填充NA?

发布于 2024-10-28 07:23:17 字数 2037 浏览 3 评论 0原文

我有一个 xts 对象,我使用 to.period() 来“增加”周期以生成第二个 xts 对象。然后我将两个 xts 对象组合回更快的时期。如何设置 cbind() 以便 NA(见下文)被替换并填充为最新值?

require(xts)
data(sample_matrix)
x <- as.xts(sample_matrix)
x.wk <- to.weekly(x)
x2 <- cbind(x, x.wk[ , 4])
print(first(x2, "2 weeks"))

>head(x2)
                   Open     High      Low    Close  x.Close
    2007-01-02 50.03978 50.11778 49.95041 50.11778       NA
    2007-01-03 50.23050 50.42188 50.23050 50.39767       NA
    2007-01-04 50.42096 50.42096 50.26414 50.33236       NA
    2007-01-05 50.37347 50.37347 50.22103 50.33459       NA
    2007-01-06 50.24433 50.24433 50.11121 50.18112       NA
    2007-01-07 50.13211 50.21561 49.99185 49.99185 49.99185
    2007-01-08 50.03555 50.10363 49.96971 49.98806       NA
    2007-01-09 49.99489 49.99489 49.80454 49.91333       NA
    2007-01-10 49.91228 50.13053 49.91228 49.97246       NA
    2007-01-11 49.88529 50.23910 49.88529 50.23910       NA
    2007-01-12 50.21258 50.35980 50.17176 50.28519       NA
    2007-01-13 50.32385 50.48000 50.32385 50.41286       NA
    2007-01-14 50.46359 50.62395 50.46359 50.60145 50.60145

>head(x3)    # this is what I want instead
                   Open     High      Low    Close  x.Close
    2007-01-02 50.03978 50.11778 49.95041 50.11778       NA
    2007-01-03 50.23050 50.42188 50.23050 50.39767       NA
    2007-01-04 50.42096 50.42096 50.26414 50.33236       NA
    2007-01-05 50.37347 50.37347 50.22103 50.33459       NA
    2007-01-06 50.24433 50.24433 50.11121 50.18112       NA
    2007-01-07 50.13211 50.21561 49.99185 49.99185 49.99185
    2007-01-08 50.03555 50.10363 49.96971 49.98806 49.99185
    2007-01-09 49.99489 49.99489 49.80454 49.91333 49.99185
    2007-01-10 49.91228 50.13053 49.91228 49.97246 49.99185
    2007-01-11 49.88529 50.23910 49.88529 50.23910 49.99185
    2007-01-12 50.21258 50.35980 50.17176 50.28519 49.99185
    2007-01-13 50.32385 50.48000 50.32385 50.41286 49.99185
    2007-01-14 50.46359 50.62395 50.46359 50.60145 50.60145

谢谢你!

I have an xts object, which I used to.period() on to 'up' the period to produce a second xts object. Then I combined the two xts objects back to the faster period. How can I set cbind() so that the NA's (see below) are replaced and filled with the latest value?

require(xts)
data(sample_matrix)
x <- as.xts(sample_matrix)
x.wk <- to.weekly(x)
x2 <- cbind(x, x.wk[ , 4])
print(first(x2, "2 weeks"))

>head(x2)
                   Open     High      Low    Close  x.Close
    2007-01-02 50.03978 50.11778 49.95041 50.11778       NA
    2007-01-03 50.23050 50.42188 50.23050 50.39767       NA
    2007-01-04 50.42096 50.42096 50.26414 50.33236       NA
    2007-01-05 50.37347 50.37347 50.22103 50.33459       NA
    2007-01-06 50.24433 50.24433 50.11121 50.18112       NA
    2007-01-07 50.13211 50.21561 49.99185 49.99185 49.99185
    2007-01-08 50.03555 50.10363 49.96971 49.98806       NA
    2007-01-09 49.99489 49.99489 49.80454 49.91333       NA
    2007-01-10 49.91228 50.13053 49.91228 49.97246       NA
    2007-01-11 49.88529 50.23910 49.88529 50.23910       NA
    2007-01-12 50.21258 50.35980 50.17176 50.28519       NA
    2007-01-13 50.32385 50.48000 50.32385 50.41286       NA
    2007-01-14 50.46359 50.62395 50.46359 50.60145 50.60145

>head(x3)    # this is what I want instead
                   Open     High      Low    Close  x.Close
    2007-01-02 50.03978 50.11778 49.95041 50.11778       NA
    2007-01-03 50.23050 50.42188 50.23050 50.39767       NA
    2007-01-04 50.42096 50.42096 50.26414 50.33236       NA
    2007-01-05 50.37347 50.37347 50.22103 50.33459       NA
    2007-01-06 50.24433 50.24433 50.11121 50.18112       NA
    2007-01-07 50.13211 50.21561 49.99185 49.99185 49.99185
    2007-01-08 50.03555 50.10363 49.96971 49.98806 49.99185
    2007-01-09 49.99489 49.99489 49.80454 49.91333 49.99185
    2007-01-10 49.91228 50.13053 49.91228 49.97246 49.99185
    2007-01-11 49.88529 50.23910 49.88529 50.23910 49.99185
    2007-01-12 50.21258 50.35980 50.17176 50.28519 49.99185
    2007-01-13 50.32385 50.48000 50.32385 50.41286 49.99185
    2007-01-14 50.46359 50.62395 50.46359 50.60145 50.60145

Thank you!

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

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

发布评论

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

评论(2

硪扪都還晓 2024-11-04 07:23:17

合并两个对象后,您只需使用 na.locf 即可。

> head(na.locf(x2),15)
               Open     High      Low    Close  x.Close
2007-01-02 50.03978 50.11778 49.95041 50.11778       NA
2007-01-03 50.23050 50.42188 50.23050 50.39767       NA
2007-01-04 50.42096 50.42096 50.26414 50.33236       NA
2007-01-05 50.37347 50.37347 50.22103 50.33459       NA
2007-01-06 50.24433 50.24433 50.11121 50.18112       NA
2007-01-07 50.13211 50.21561 49.99185 49.99185 49.99185
2007-01-08 50.03555 50.10363 49.96971 49.98806 49.99185
2007-01-09 49.99489 49.99489 49.80454 49.91333 49.99185
2007-01-10 49.91228 50.13053 49.91228 49.97246 49.99185
2007-01-11 49.88529 50.23910 49.88529 50.23910 49.99185
2007-01-12 50.21258 50.35980 50.17176 50.28519 49.99185
2007-01-13 50.32385 50.48000 50.32385 50.41286 49.99185
2007-01-14 50.46359 50.62395 50.46359 50.60145 50.60145
2007-01-15 50.61724 50.68583 50.47359 50.48912 50.60145
2007-01-16 50.62024 50.73731 50.56627 50.67835 50.60145

You just need to use na.locf after you've merged the two objects.

> head(na.locf(x2),15)
               Open     High      Low    Close  x.Close
2007-01-02 50.03978 50.11778 49.95041 50.11778       NA
2007-01-03 50.23050 50.42188 50.23050 50.39767       NA
2007-01-04 50.42096 50.42096 50.26414 50.33236       NA
2007-01-05 50.37347 50.37347 50.22103 50.33459       NA
2007-01-06 50.24433 50.24433 50.11121 50.18112       NA
2007-01-07 50.13211 50.21561 49.99185 49.99185 49.99185
2007-01-08 50.03555 50.10363 49.96971 49.98806 49.99185
2007-01-09 49.99489 49.99489 49.80454 49.91333 49.99185
2007-01-10 49.91228 50.13053 49.91228 49.97246 49.99185
2007-01-11 49.88529 50.23910 49.88529 50.23910 49.99185
2007-01-12 50.21258 50.35980 50.17176 50.28519 49.99185
2007-01-13 50.32385 50.48000 50.32385 50.41286 49.99185
2007-01-14 50.46359 50.62395 50.46359 50.60145 50.60145
2007-01-15 50.61724 50.68583 50.47359 50.48912 50.60145
2007-01-16 50.62024 50.73731 50.56627 50.67835 50.60145
表情可笑 2024-11-04 07:23:17

这里有两个例子来做到这一点。

第一种是函数式编程方式:

x2$x <- Reduce(function(x,y){
  if(is.na(y)) c(x,x[length(x)]) else c(x,y)},
  as.numeric(x2$x.Close)
  )

另一种是过程式编程方式:

z <- x2$x.Close
z1 <- rle(c(is.na(z)))
z2 <- z1$lengths[z1$values==TRUE] +1
z3 <- z[!is.na(z)]

# in the case that first row is NA
if (is.na(z[1])) {
  z3 <- c(NA, z3)
  z2[1] <- z2[1]-1
  }

# in the case that the last row is not NA
if (!is.na(z[length(z)])) z2 <- c(z2, 1)
z4 <- rep(z3, z2)
x2$x <- z4

Here are two examples to do that.

The first is a kind of functional programing way:

x2$x <- Reduce(function(x,y){
  if(is.na(y)) c(x,x[length(x)]) else c(x,y)},
  as.numeric(x2$x.Close)
  )

The other is a kind of procedural way:

z <- x2$x.Close
z1 <- rle(c(is.na(z)))
z2 <- z1$lengths[z1$values==TRUE] +1
z3 <- z[!is.na(z)]

# in the case that first row is NA
if (is.na(z[1])) {
  z3 <- c(NA, z3)
  z2[1] <- z2[1]-1
  }

# in the case that the last row is not NA
if (!is.na(z[length(z)])) z2 <- c(z2, 1)
z4 <- rep(z3, z2)
x2$x <- z4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文