如何创建和合并多个XTS对象

发布于 2025-01-22 12:31:12 字数 172 浏览 2 评论 0原文

我想在循环中合并许多XTS对象。为了这样做,我必须编写以下代码138次。 temp = merge(temp,temp1)temp = merge(temp,temp2)temp = merge(temp,temp3)temp = merge(temp,temp4)> 有没有办法自动执行该过程?

I want to merge MANY xts objects in a loop. In order to do so, i have to write the following code 138 times.
temp=merge(temp,temp1) temp=merge(temp,temp2) temp=merge(temp,temp3) temp=merge(temp,temp4)
is there a way to automate that procedure?

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

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

发布评论

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

评论(1

梦途 2025-01-29 12:31:12

创建一个包含对象的列表,然后使用do.call/merge。在下面,如果XTS对象本身具有列名,则可以省略列表的组件名称。尽管在此示例中,日期是相同的,如果不相同,它仍然可以正常工作。

library(xts)

# test data
x1 <- x2 <- x3 <- xts(1:3, as.Date("2000-01-01") + 0:2)

L <- list(x1 = x1, x2 = x2, x3 = x3)
do.call("merge", L)

给予:

           x1 x2 x3
2000-01-01  1  1  1
2000-01-02  2  2  2
2000-01-03  3  3  3

如果XTS对象具有其名称的模式,例如它们是唯一以x开头的对象,那么此替代方案也可以创建L:

L <- mget(ls(pattern = "^x"))

或者如果它们是唯一的XTS对象,那么这将有效地创建列表。

L <- Filter(is.xts, mget(ls()))

Create a list which contains the objects and then use do.call/merge. Below we could hae omitted the component names of the list if the xts objects themselves had column names. Although in this example the dates are the same it will still work if they are not.

library(xts)

# test data
x1 <- x2 <- x3 <- xts(1:3, as.Date("2000-01-01") + 0:2)

L <- list(x1 = x1, x2 = x2, x3 = x3)
do.call("merge", L)

giving:

           x1 x2 x3
2000-01-01  1  1  1
2000-01-02  2  2  2
2000-01-03  3  3  3

If the xts objects have a pattern to their names, e.g. they are the only objects whose names start with x then this alternative would also work to create L:

L <- mget(ls(pattern = "^x"))

or if they are the only xts objects then this would work to create the list.

L <- Filter(is.xts, mget(ls()))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文