Dong.part <- matrix(c(39100, 40900, 41000, 43900, 44100, 44300, 47200, 48800, 48000,
49700, 49850, 47700, 48200, 45850, 44800, 44300, 43150, 43350,
43550, 42800, 43200, 41550, 40900, 41250, 42000, 42550, 42400,
42150, 43500, 42150, 40850, 39950, 41000, 41650, 41000, 41800,
41700, 41300, 42400, 42200, 44550, 45300, 45450, 46200, 46300,
46300, 46600, 47100, 46350, 46000, 45900, 47900, 50000, 52800,
52100, 51700, 52300, 51600, 50100, 51100, 54000, 56400, 58200,
57500, 59700, 60300, 59600, 60500, 61900, 61200, 62500, 62500,
65100, 66400, 66000, 67800, 69200, 72200, 71000, 71100, 70500,
65000, 65400, 60800, 60500, 63000, 62100, 62000, 64000, 64200,
62500, 63200, 63200, 66900, 66400, 67000, 69900, 70200, 69900,
71700, 73000, 76400, 74700, 74500, 73000, 75900, 79500, 84800,
84500, 87400, 88500, 88300, 88200, 89800, 88000, 85200, 86300,
81600, 76500, 82700, 82500, 82000, 80400, 81000, 80000, 76300,
81800, 79300, 79400, 78200, 78900, 82000, 83000, 85200, 87000,
86100, 84700, 86000, 84800, 86200, 88500, 89000, 89500, 88000,
87800, 87000, 84200, 82500, 80200, 74000, 65200, 67800, 70800,
70000, 73300, 76300, 76000, 73800, 67000, 64400, 63000, 62500,
62500, 66800, 68800, 69000, 68700, 69300, 69400, 72000, 71700,
69900, 71000, 70100, 68800, 69000, 68800, 67500, 68000, 68000,
69800, 68900, 68800, 68700, 67500, 67900, 69800, 70200, 69600,
69500, 68200, 68000, 67000, 67800, 66800, 68900, 68400, 68700,
68900, 70500, 70400, 70500, 72800, 74100, 72500, 71500, 71900,
72100, 69600, 69600, 68000, 66800, 66700, 66600, 66400, 65000,
63000, 63400, 62100, 60300, 58100, 61200, 64800, 64800, 64900,
63800, 63500, 61900, 62000, 61000, 62100, 61000, 60500, 60800,
60400, 60000, 59100, 57900, 54200, 52700, 52100, 51600, 50200,
51100, 50700, 47900, 48300, 48150, 51800, 54000, 52600, 53000,
54500, 54000, 53000, 53100, 52700, 55800, 58300, 58100, 57600,
56700, 57200, 58500, 59500, 62500, 62600, 62900, 62000, 61000,
59500, 60300, 58000, 57000, 57000, 59000, 59200, 57700, 56700,
54800, 57400, 58400, 57500, 57300, 58300, 57000, 56700, 55800,
56100, 56400, 56000, 56500, 55800, 56500, 58800, 60000, 60400,
60400, 62200, 69900, 70300), ncol=1)
dummy <- seq(0,0, length.out=nrow(Dong.part))
Dong.part <- cbind(Dong.part, dummy)
for(i in 100:nrow(Dong.part)){
if(Dong.part[i,1]<= min(Dong.part[i-99:i-1,1])){
Dong.part[i,4] <- 1
}
}
这是股票价格和虚拟是我所做的。 我想比较每个长度为99的区间的股票价格。
如果股票价格(位于第一列)是长度为99的区间中的最低价格,我想在该行将虚拟值更改为1.
然而,它根本不起作用。我怎样才能做好这项工作?
答案 0 :(得分:2)
我猜你忘记了围绕i-99
和i-1
的圆括号;假人也是第2列(没有第4列):
for(i in 100:nrow(Dong.part)){
if(Dong.part[i,1] <= min(Dong.part[(i-99):(i-1),1])){
Dong.part[i,2] <- 1
}
}