2016年5月21日土曜日

how to convert from CSV to XTS

when time series data is given in CSV format as below.

~$ head SP5.csv 
Date,Open,High,Low,Close,Volume,Adj Close
2016-05-02,2067.169922,2084.870117,2025.910034,2052.320068,4068314600,2052.320068
2016-04-01,2056.620117,2111.050049,2033.800049,2065.300049,4087129000,2065.300049
2016-03-01,1937.089966,2072.209961,1937.089966,2059.73999,4379759000,2059.73999

the combination of read.csv, read.zoo and as.xts is able to transform and conver t data into xts objects.

> SP5 <- as.xts(read.zoo(read.csv("~/SP5.csv")))
> tail(SP5)
              Open    High     Low   Close     Volume Adj.Close
2015-12-01 2082.93 2104.27 1993.26 2043.94 3922935900   2043.94
2016-01-04 2038.20 2038.20 1812.29 1940.24 5153017800   1940.24
2016-02-01 1936.94 1962.96 1810.10 1932.23 4881887000   1932.23
2016-03-01 1937.09 2072.21 1937.09 2059.74 4379759000   2059.74
2016-04-01 2056.62 2111.05 2033.80 2065.30 4087129000   2065.30
2016-05-02 2067.17 2084.87 2025.91 2052.32 4068314600   2052.32
> class(SP5)
[1] "xts" "zoo"

plot() function and frequency conversion function like to.monthly() are available as usual.

> plot(to.yearly(SP5))


0 件のコメント: