我有一些股票数据(如下所示)。现在我想创建一个二进制列(1或0值),显示当前价格是否高于前90天价格,1表示是,0表示否。
如何有效地实现这一目标?我几乎陷入困境。
股票数据
Open High Low Close
2007-01-01 137.00 137.00 137.00 137.00
2007-01-02 137.00 138.50 134.75 136.50
2007-01-03 115.00 116.75 115.00 115.50
2007-01-04 114.50 114.50 111.00 111.75
2007-01-05 112.00 112.50 111.00 112.25
2007-01-08 112.00 112.00 110.50 112.00
2007-01-09 112.00 112.50 110.50 110.50
2007-01-10 110.75 115.25 109.50 112.75
2007-01-11 114.25 118.50 114.00 118.50
2007-01-12 118.25 119.50 115.50 117.00
2007-01-15 117.75 119.00 117.50 118.75
2007-01-16 118.50 122.00 117.50 119.50
2007-01-17 120.00 121.00 119.50 120.50
2007-01-18 120.25 125.50 120.25 124.50
2007-01-19 123.75 126.25 123.25 126.00
2007-01-22 126.25 126.75 123.25 124.50
2007-01-23 123.75 124.25 122.50 124.25
2007-01-24 125.00 125.75 123.75 125.25
2007-01-25 127.25 128.25 125.75 126.50
2007-01-26 126.25 133.75 125.00 128.50
2007-01-29 129.25 129.50 126.00 128.50
2007-01-30 128.00 130.00 125.75 129.75
2007-01-31 129.25 132.00 129.00 131.50
2007-02-01 132.00 133.50 130.00 130.00
2007-02-02 129.50 132.50 129.25 132.00
2007-02-05 133.00 133.50 131.00 131.50
2007-02-06 132.00 133.50 131.00 131.25
2007-02-07 132.00 137.25 130.50 136.25
2007-02-08 135.00 138.00 134.00 136.00
2007-02-09 136.00 138.75 136.00 138.75
2007-02-12 137.75 137.75 135.50 136.75
2007-02-13 137.25 140.00 137.25 139.50
2007-02-14 159.00 170.50 157.00 169.00
2007-02-15 167.50 174.50 167.00 169.50
2007-02-16 166.50 169.00 162.50 166.50
2007-02-19 166.00 171.50 165.00 168.50
2007-02-20 168.00 169.50 166.00 167.50
2007-02-21 167.50 170.50 165.50 166.50
2007-02-22 168.00 168.00 166.00 166.00
2007-02-23 168.00 168.00 165.50 166.00
2007-02-26 167.50 168.00 166.00 166.50
2007-02-27 166.00 166.50 157.50 160.50
2007-02-28 157.00 160.00 155.00 157.00
2007-03-01 158.00 159.50 152.50 154.00
2007-03-02 156.00 158.00 154.00 156.00
2007-03-05 152.00 157.00 150.50 156.50
2007-03-06 158.00 161.50 157.00 157.00
2007-03-07 159.00 161.00 156.00 158.00
2007-03-08 160.00 167.50 159.50 163.50
2007-03-09 164.50 170.50 162.50 168.50
2007-03-12 169.50 172.00 165.50 167.00
2007-03-13 168.50 169.00 161.50 161.50
2007-03-14 157.50 160.00 153.00 153.00
2007-03-15 156.00 162.00 156.00 162.00
2007-03-16 161.50 167.50 161.50 163.50
2007-03-19 166.50 168.00 165.50 167.00
2007-03-20 167.50 171.50 166.00 171.00
2007-03-21 171.50 172.00 169.00 170.50
2007-03-22 173.00 176.00 171.00 173.50
2007-03-23 173.50 177.50 171.00 175.50
2007-03-26 174.50 175.50 169.00 169.50
2007-03-27 171.50 177.00 170.00 177.00
2007-03-28 175.50 177.50 171.00 172.00
2007-03-29 173.50 175.00 172.00 174.50
2007-03-30 176.00 178.00 175.50 177.00
2007-04-02 175.00 181.50 175.00 179.00
2007-04-03 181.00 184.00 180.50 182.50
2007-04-04 183.50 183.50 175.50 176.00
2007-04-05 177.00 177.00 170.50 173.00
2007-04-06 173.00 173.00 173.00 173.00
2007-04-09 173.00 173.00 173.00 173.00
2007-04-10 173.50 176.00 171.50 175.00
2007-04-11 179.00 184.00 177.00 177.50
2007-04-12 179.00 180.50 175.50 180.00
2007-04-13 179.50 188.50 179.00 185.50
2007-04-16 185.00 186.00 181.00 184.00
2007-04-17 180.00 184.50 179.00 182.50
2007-04-18 180.50 182.50 179.00 180.00
2007-04-19 179.00 183.50 176.50 183.50
2007-04-20 184.00 188.00 183.00 186.50
2007-04-23 187.50 190.00 187.00 190.00
2007-04-24 189.00 190.00 183.50 184.00
2007-04-25 185.50 189.50 185.00 188.00
2007-04-26 177.50 182.00 174.00 175.00
2007-04-27 175.00 181.50 173.00 176.00
2007-04-30 174.50 178.00 172.00 176.00
2007-05-01 176.00 176.00 176.00 176.00
2007-05-02 175.00 179.00 174.50 176.50
2007-05-03 177.00 177.50 172.00 175.50
2007-05-04 174.00 177.00 173.50 174.50
2007-05-07 175.00 177.00 174.50 176.50
2007-05-08 177.00 178.00 172.50 173.00
2007-05-09 173.00 176.50 172.00 176.50
2007-05-10 176.00 177.50 172.50 174.00
2007-05-11 172.50 174.50 167.50 174.00
2007-05-14 173.50 174.50 169.00 171.00
2007-05-15 169.00 169.50 163.50 169.50
2007-05-16 169.00 174.00 167.50 174.00
2007-05-17 174.00 174.00 174.00 174.00
2007-05-18 174.50 174.50 170.00 172.50
答案 0 :(得分:2)
在这种情况下,您可以使用rollapply
或rollmax
执行此操作。
require(xts)
data(sample_matrix)
x <- as.xts(sample_matrix)
# rollmaxr ensures the data are aligned to the right side of the window
x$checkpoint <- x$Close > rollmaxr(lag(x$Close), 90)