将一个 xts 除以另一个并在此过程中创建一个新的 xts [英] Dividing one xts by another and creating a new xts in the process

查看:21
本文介绍了将一个 xts 除以另一个并在此过程中创建一个新的 xts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将两个 xts 对象彼此分开.每个 xts 具有相同的列数(具有相同的列名)但行数不同.我希望我的代码在其他 xts 中找到其各自的列并找到相应的日期值并将它们相除,然后将新输出放入具有相同列的新 xts 中.

I'm looking to divide two xts objects by each other. Each xts has the same number of columns (with the same column names) but different numbers of rows. I would like my code to find its respective column in the the other xts and locate the respective date value and divide them then put the new output in a new xts with the same column.

例如:

xts 1

Date         V1     V2
2017-07-21   4.5    NA
2017-09-15   NA     2.5

xts 2

Date         V1     V2
2017-06-15  12.9   10.7
2017-07-21   6.7    2.2
2017-08-13   7.9    8.3
2017-09-15   4.5    3.2

新 xts

Date         V1     V2
2017-07-21  0.67    NA
2017-09-15   NA    1.28

推荐答案

我认为 xts 库实际上是一个非常了不起的工具,它会自动做到这一点

I think the xts library is actually a pretty amazing tool and it does that automatically

> xts1 <- structure(c(4.5, NA, NA, 2.5), .Dim = c(2L, 2L),
   .Dimnames = list(NULL, c("V1", "V2")),
   index = structure(c(1500595200, 1505433600), tzone = "UTC", tclass = "Date"),
   class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date",
   .indexTZ = "UTC", tzone = "UTC")
> xts2 <- structure(c(12.9, 6.7, 7.9, 4.5, 10.7, 2.2, 8.3, 3.2),
   .Dim = c(4L, 2L), .Dimnames = list(NULL, c("V1", "V2")),
   index = structure(c(1497484800, 1500595200, 1502582400, 1505433600),
       tzone = "UTC", tclass = "Date"),
   class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date",
   .indexTZ = "UTC", tzone = "UTC")
> xts1 / xts2
                  V1      V2
2017-07-21 0.6716418      NA
2017-09-15        NA 0.78125

算术运算会尊重日期并在做任何事情之前匹配它们.

Arithmetic operations will respect dates and match them before doing anything.

希望有帮助

这篇关于将一个 xts 除以另一个并在此过程中创建一个新的 xts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆