Convert data frame with date column to timeseries

后端 未结 5 888
走了就别回头了
走了就别回头了 2020-11-29 20:06

I\'ve got a data frame with the following data:

>PRICE
         DATE  CLOSE
1    20070103 54.700
2    20070104 54.770
3    20070105 55.120
4    20070108 5         


        
5条回答
  •  庸人自扰
    2020-11-29 21:02

    Most people find working with the time series class to be a big pain. You should consider using the zoo class from package zoo. It will not complain about missing times , only about duplicates. The PerformanceAnalytics functions are almost certainly going to be expecting 'zoo' or its descendant class 'xts'.

    pricez <- read.zoo(text="   DATE  CLOSE
     1    20070103 54.700
     2    20070104 54.770
     3    20070105 55.120
     4    20070108 54.870
     5    20070109 54.860
     6    20070110 54.270
     7    20070111 54.770
     8    20070112 55.360
     9    20070115 55.760
     ")
     index(pricez) <- as.Date(as.character(index(pricez)), format="%Y%m%d")
     pricez
    2007-01-03 2007-01-04 2007-01-05 2007-01-08 2007-01-09 2007-01-10 2007-01-11 2007-01-12 2007-01-15 
         54.70      54.77      55.12      54.87      54.86      54.27      54.77      55.36      55.76 
    

提交回复
热议问题