如何计算R中两个日期之间的年份?

时间:2018-06-20 18:27:00

标签: r dataframe time-series finance yahoo-finance

我想计算两个日期之间的年差。这是一些测试数据;

               O      H      L      C  StoHH  StoLL         K        kM
2007-01-02 6220.8 6312.5 6220.8 6310.9    0.0    0.0 0.0000000 0.0000000
2007-01-03 6310.9 6322.0 6296.0 6319.0    0.0    0.0 0.0000000 0.0000000
2007-01-04 6319.0 6319.0 6261.0 6287.0    0.0    0.0 0.0000000 0.0000000
2007-01-05 6287.0 6287.0 6220.1 6220.1    0.0    0.0 0.0000000 0.0000000
2007-01-08 6220.1 6246.0 6187.0 6194.2    0.0    0.0 0.0000000 0.0000000
2007-01-09 6194.2 6218.5 6190.4 6196.1    0.0    0.0 0.0000000 0.0000000
2007-01-10 6196.1 6196.1 6142.0 6160.7    0.0    0.0 0.0000000 0.0000000
2007-01-11 6160.7 6233.1 6130.2 6230.1    0.0    0.0 0.0000000 0.0000000
2007-01-12 6230.1 6247.6 6204.3 6239.0    0.0    0.0 0.0000000 0.0000000
2007-01-15 6239.0 6279.7 6239.0 6263.5    0.0    0.0 0.0000000 0.0000000
2007-01-16 6263.5 6266.4 6206.4 6215.7    0.0    0.0 0.0000000 0.0000000
2007-01-17 6215.7 6227.0 6163.5 6204.5    0.0    0.0 0.0000000 0.0000000
2007-01-18 6204.5 6257.2 6204.5 6210.3    0.0    0.0 0.0000000 0.0000000
2007-01-19 6210.3 6243.3 6178.0 6237.2 6322.0 6130.2 0.5578734 0.1859578
2007-01-22 6237.2 6270.9 6215.8 6218.4 6322.0 6130.2 0.4598530 0.3392421
2007-01-23 6218.4 6240.7 6189.2 6227.6 6319.0 6130.2 0.5158898 0.5112054
2007-01-24 6227.6 6320.9 6227.6 6314.8 6320.9 6130.2 0.9680120 0.6479183
2007-01-25 6314.8 6335.1 6262.1 6269.3 6335.1 6130.2 0.6788662 0.7209227
2007-01-26 6269.3 6271.4 6226.4 6228.0 6335.1 6130.2 0.4773053 0.7080612
2007-01-29 6228.0 6253.7 6216.0 6239.9 6335.1 6130.2 0.5353819 0.5638511
                  Trs        Trl      TrsCum     TrlCum  CumBal  CumRet
2007-01-02     0.0000     0.0000      0.0000     0.0000 1000000 1.00000
2007-01-03   810.0098  -810.0098    810.0098  -810.0098 1000000 1.00000
2007-01-04 -3200.0000  3200.0000  -2389.9902  2389.9902 1000000 1.00000
2007-01-05 -6689.9902  6689.9902  -9079.9804  9079.9804 1000000 1.00000
2007-01-08 -2589.9903  2589.9903 -11669.9707 11669.9707 1000000 1.00000
2007-01-09   189.9903  -189.9903 -11479.9804 11479.9804 1000000 1.00000
2007-01-10 -3539.9903  3539.9903 -15019.9707 15019.9707 1000000 1.00000
2007-01-11  6939.9903 -6939.9903  -8079.9804  8079.9804 1000000 1.00000
2007-01-12   889.9902  -889.9902  -7189.9902  7189.9902 1000000 1.00000
2007-01-15  2450.0000 -2450.0000  -4739.9902  4739.9902 1000000 1.00000
2007-01-16 -4779.9805  4779.9805  -9519.9707  9519.9707 1000000 1.00000
2007-01-17 -1120.0195  1120.0195 -10639.9902 10639.9902 1000000 1.00000
2007-01-18   579.9805  -579.9805 -10060.0097 10060.0097 1000000 1.00000
2007-01-19  2690.0390 -2690.0390  -7369.9707  7369.9707 1000000 1.00000
2007-01-22 -1880.0293     0.0000  -9250.0000  7369.9707  998120 0.99812
2007-01-23   920.0196     0.0000  -8329.9804  7369.9707  999040 0.99904
2007-01-24  8719.9707     0.0000    389.9903  7369.9707 1007760 1.00776
2007-01-25 -4550.0000     0.0000  -4160.0097  7369.9707 1003210 1.00321
2007-01-26     0.0000  4129.9805  -4160.0097 11499.9512 1007340 1.00734
2007-01-29     0.0000 -1189.9902  -4160.0097 10309.9610 1006150 1.00615
                 UlcRi UlcInd     PFctL     PFctW
2007-01-02         Inf      0     0.000    0.0000
2007-01-03         Inf      0     0.000    0.0000
2007-01-04         Inf      0     0.000    0.0000
2007-01-05         Inf      0     0.000    0.0000
2007-01-08         Inf      0     0.000    0.0000
2007-01-09         Inf      0     0.000    0.0000
2007-01-10         Inf      0     0.000    0.0000
2007-01-11         Inf      0     0.000    0.0000
2007-01-12         Inf      0     0.000    0.0000
2007-01-15         Inf      0     0.000    0.0000
2007-01-16         Inf      0     0.000    0.0000
2007-01-17         Inf      0     0.000    0.0000
2007-01-18         Inf      0     0.000    0.0000
2007-01-19  0.00000000      0     0.000    0.0000
2007-01-22 -0.18800293      0 -1880.029    0.0000
2007-01-23 -0.09600097      0     0.000  920.0196
2007-01-24  0.00000000      0     0.000 8719.9707
2007-01-25 -0.45149641      0 -4550.000    0.0000
2007-01-26 -0.04167853      0     0.000 4129.9805
2007-01-29 -0.15976123      0 -1189.990    0.0000

到目前为止,我所做的是;

    sapply(a.1, class)
            O         H         L         C     StoHH     StoLL         K        kM 
    "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 
          Trs       Trl    TrsCum    TrlCum    CumBal    CumRet     UlcRi    UlcInd 
    "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 
        PFctL     PFctW 
    "numeric" "numeric" 

    time(a.1)
    Error in hasTsp(x) : invalid time series parameters specified

 as.yearmon(time(a.1))
Error in hasTsp(x) : invalid time series parameters specified
time(a.1[1,])
Error in hasTsp(x) : invalid time series parameters specified
as.numeric(a.1)
Error: (list) object cannot be coerced to type 'double'
a.1 <- data.frame(a.1)

我想做的是从ts的开始和结束日期算起以十进制表示的年数。我曾尝试将其转换为df并使用其他方法,但似乎无法弄清楚如何将ts列转换为可延展的形式以执行计算,有什么想法吗?

0 个答案:

没有答案