我有以下矩阵:
B = as.POSIXct("2015-06-21") + seq(1,18)
P = c(21.675,21.775,21.675,21.725,21.575,21.525,21.625,21.475,21.375,21.325,21.32,21.475,21.375,21.325,21.425,21.525,21.275,21.325)
A = c(-1,1,-1,1,-1,0,1,-1,0,0,0,1,-1,0,1,0,-1,1)
C = c(rep(0,15),1,0,0)
D = c(0,0,0,0,0,-1,0,0,-1,-1,-1,0,0,-1,0,0,0,0)
E = seq(1,18)
dat = as.data.frame(cbind(P,A,C,D))
rownames(dat) = B
输出矩阵如下所示:
P A C D E
2015-06-21 00:00:01 21.675 -1 0 0 1
2015-06-21 00:00:02 21.775 1 0 0 2
2015-06-21 00:00:03 21.675 -1 0 0 3
2015-06-21 00:00:04 21.725 1 0 0 4
2015-06-21 00:00:05 21.575 -1 0 0 5
2015-06-21 00:00:06 21.525 0 0 -1 6
2015-06-21 00:00:07 21.625 1 0 0 7
2015-06-21 00:00:08 21.475 -1 0 0 8
2015-06-21 00:00:09 21.375 0 0 -1 9
2015-06-21 00:00:10 21.325 0 0 -1 10
2015-06-21 00:00:11 21.325 0 0 -1 11
2015-06-21 00:00:12 21.475 1 0 0 12
2015-06-21 00:00:13 21.375 -1 0 0 13
2015-06-21 00:00:14 21.325 0 0 -1 14
2015-06-21 00:00:15 21.425 1 0 0 15
2015-06-21 00:00:16 21.525 0 1 0 16
2015-06-21 00:00:17 21.275 -1 0 0 17
2015-06-21 00:00:18 21.325 1 0 0 18
A列的交替-1和1s。目标是查看列A,其中有零,并查看是否可以使用C和D替换它们。
E列表示行号,P列表示价格信息。列表示本地最小和最大点(-1和1)。
因此,在第4行和第7行之间,第6行中有一个零.4和7表示本地最大点数为21.725和21.625。
我想检查第5行是否确实是第4行和第7行之间的最小数字。结果,第6行的值较低,21.525与第5行的21.575相比。
所以,我想将第A列的第5个条目从-1和第6个条目替换为0从0替换为-1。
我想通过上述逻辑来完成矩阵,并最终得到以下结果:
P A C D E
2015-06-21 00:00:01 21.675 -1 0 0 1
2015-06-21 00:00:02 21.775 1 0 0 2
2015-06-21 00:00:03 21.675 -1 0 0 3
2015-06-21 00:00:04 21.725 1 0 0 4
2015-06-21 00:00:05 21.575 0 0 0 5
2015-06-21 00:00:06 21.525 -1 0 -1 6
2015-06-21 00:00:07 21.625 1 0 0 7
2015-06-21 00:00:08 21.475 0 0 0 8
2015-06-21 00:00:09 21.375 0 0 -1 9
2015-06-21 00:00:10 21.325 -1 0 -1 10
2015-06-21 00:00:11 21.325 0 0 -1 11
2015-06-21 00:00:12 21.475 1 0 0 12
2015-06-21 00:00:13 21.375 0 0 0 13
2015-06-21 00:00:14 21.325 -1 0 -1 14
2015-06-21 00:00:15 21.425 0 0 0 15
2015-06-21 00:00:16 21.525 1 1 0 16
2015-06-21 00:00:17 21.275 -1 0 0 17
2015-06-21 00:00:18 21.325 1 0 0 18
我在编写和有效的代码时遇到了麻烦。任何帮助将不胜感激!