Read timeseries from csv file with xts or quantmod
On Fri, 24 Jul 2009, Mark Breman wrote:
Hi, I have a timeseries in a csv file that I want to read into an xts object.
You can use read.zoo() from the "zoo" package and the as.xts() to coerce
to an "xts" series.
library("xts")
x <- as.xts(read.zoo("US_PMI_Monthly.csv", dec = ","))
Note that you have to set the decimal separator appropriately.
hth,
Z
The csv file looks like: 1948-01-01 51,7 1948-02-01 50,2 1948-03-01 43,3 1948-04-01 45,4 1948-05-01 49,5 1948-06-01 53 1948-07-01 48,4 1948-08-01 45,1 1948-09-01 42,1 1948-10-01 47,2 1948-11-01 42,4 1948-12-01 35 1949-01-01 32,9 1949-02-01 31,3 1949-03-01 34,5 1949-04-01 35,5 1949-05-01 32,6 1949-06-01 31,6 etc... I have attached it to this post. I tried to read the file with getSymbols() but it gives me an error: ------------------------------------- start trace ----------------------------------------------
library(quantmod)
getSymbols('US_PMI_Monthly', src='csv', header = FALSE, sep = "\t",
dec=",") Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent
------------------------------------ end trace ------------------------------------------------- Strange because read.csv() can read this file into a data.frame: -------------------------------------- start trace -----------------------------------------------
data = read.csv("US_PMI_Monthly.csv", header = FALSE, sep = "\t", dec=",")
class(data)
[1] "data.frame"
-------------------------------------- end trace ------------------------------------------------- I tried to be smart and convert the data.frame to an xts object, but this also gives me an error: ------------------------------------- start trace -------------------------------------------------
as.xts(data)
Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format
------------------------------------- end trace --------------------------------------------------- I am using R 2.8.1 for windows with xts 0.6-6 and quantmod 0.3-10. What am I doing wrong here? Regards, -Mark-