R循环遍历矩阵并存储附有正确日期的结果时的多个条件

时间:2022-10-19 09:54:36

I started with this matrix exercise but quickly realised that I could't find an elegant way to facilitate what I have planned:

我从这个矩阵练习开始,但很快意识到我找不到一种优雅的方式来促进我的计划:

I have three matrices (AB, CD and MN)

我有三个矩阵(AB,CD和MN)

require(xts)
set.seed(21)
A <- xts(matrix(rnorm(20,0,0.01),2,1), Sys.Date()-2:1)
colnames(A) <- c("A")
B <- xts(matrix(rnorm(20,0,0.01),2,1), Sys.Date()-2:1)
colnames(B) <- c("B")
AB <- cbind(A,B)

C <- xts(matrix(rnorm(20,0,0.01),2,1), Sys.Date()-2:1)
colnames(C) <- c("C")
D <- xts(matrix(rnorm(20,0,0.01),2,1), Sys.Date()-2:1)
colnames(D) <- c("D")
CD <- cbind(C,D)

M <- xts(matrix(rnorm(20,0,0.01),2,1), Sys.Date()-2:1)
colnames(M) <- c("M")
N <- xts(matrix(rnorm(20,0,0.01),2,1), Sys.Date()-2:1)
colnames(N) <- c("N")
MN <- cbind(M,N)

Now I want to compare the values of matrix AB with the values in matrix CD. If the value in AB is larger than the value in CD, I want to multiply the value in MN by 1 and if the value in AB is smaller than that in CD, I want to multiply the corresponding value in MN by 0.

现在我想比较矩阵AB的值和矩阵CD中的值。如果AB中的值大于CD中的值,我想将MN中的值乘以1,如果AB中的值小于CD中的值,我想将MN中的对应值乘以0。

The results are supposed to be stored in matrix XY. Instead of writing an 'if else function' for A and B (since I have much more objects to look at), I am looking for a nice loop or apply rule.

结果应该存储在矩阵XY中。而不是为A和B编写'if else函数'(因为我有更多的对象要查看),我正在寻找一个很好的循环或应用规则。

For illustration here is the desired result

为了说明,这是期望的结果

> AB
                      A             B
2014-11-28 0.0022296815  0.0005334298
2014-11-29 0.0001194865 -0.0052610564

> CD
                     C            D
2014-11-28 -0.01804041 -0.002367161
2014-11-29 -0.00964183  0.003592538

> MN
                     M           N
2014-11-28 -0.01267330 -0.01180904
2014-11-29  0.00199446 -0.01228188

> XY
                     X            Y
2014-11-28 -0.01267330  -0.01180904
2014-11-29 0.00199446   0.00000000

Further I couldn't figure out how to properly keep the dates attached. Thanks a lot.

此外,我无法弄清楚如何正确保持附加日期。非常感谢。

1 个解决方案

#1


1  

You can multiply MN with AB > CD

您可以将MN乘以AB> CD

MN*(AB > CD)
#                    M            N
#2014-11-28 0.011121659 -0.008108887
#2014-11-29 0.003625899  0.000000000

data

MN <- structure(c(0.0111216585957413, 0.00362589897053931, -0.00810888676554509, 
-0.0294378988830602), .Dim = c(2L, 2L), .Dimnames = list(NULL, 
c("M", "N")), index = structure(c(1417132800, 1417219200), tzone = "UTC", tclass = 
 "Date"), class = c("xts", 
"zoo"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC")

AB <- structure(c(0.00793013171299118, 0.00522251263825515, 0.00392997591372168, 
0.000367571298245564), .Dim = c(2L, 2L), .Dimnames = list(NULL, 
c("A", "B")), index = structure(c(1417132800, 1417219200), tzone = "UTC", 
tclass = "Date"), class = c("xts", "zoo"), .indexCLASS = "Date", .indexTZ =
"UTC", tclass = "Date", tzone = "UTC")

CD <- structure(c(0.000558773396011995, -0.0151724057696996, -0.00457953397201899, 
0.00927956667804555), .Dim = c(2L, 2L), .Dimnames = list(NULL, 
c("C", "D")), index = structure(c(1417132800, 1417219200), tzone = "UTC",
tclass = "Date"), class = c("xts", "zoo"), .indexCLASS = "Date", .indexTZ = 
"UTC", tclass = "Date", tzone = "UTC")

#1


1  

You can multiply MN with AB > CD

您可以将MN乘以AB> CD

MN*(AB > CD)
#                    M            N
#2014-11-28 0.011121659 -0.008108887
#2014-11-29 0.003625899  0.000000000

data

MN <- structure(c(0.0111216585957413, 0.00362589897053931, -0.00810888676554509, 
-0.0294378988830602), .Dim = c(2L, 2L), .Dimnames = list(NULL, 
c("M", "N")), index = structure(c(1417132800, 1417219200), tzone = "UTC", tclass = 
 "Date"), class = c("xts", 
"zoo"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC")

AB <- structure(c(0.00793013171299118, 0.00522251263825515, 0.00392997591372168, 
0.000367571298245564), .Dim = c(2L, 2L), .Dimnames = list(NULL, 
c("A", "B")), index = structure(c(1417132800, 1417219200), tzone = "UTC", 
tclass = "Date"), class = c("xts", "zoo"), .indexCLASS = "Date", .indexTZ =
"UTC", tclass = "Date", tzone = "UTC")

CD <- structure(c(0.000558773396011995, -0.0151724057696996, -0.00457953397201899, 
0.00927956667804555), .Dim = c(2L, 2L), .Dimnames = list(NULL, 
c("C", "D")), index = structure(c(1417132800, 1417219200), tzone = "UTC",
tclass = "Date"), class = c("xts", "zoo"), .indexCLASS = "Date", .indexTZ = 
"UTC", tclass = "Date", tzone = "UTC")