[R-SIG-Finance] Read timeseries from csv file with xts or quantmod

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jul 24 17:07:44 CEST 2009


Try this:

Lines <- "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"
library(xts)
# z <- read.zoo("myfile.txt", dec = ",")
z <- read.zoo(textConnection(Lines), dec = ",")
x <- as.xts(z)


On Fri, Jul 24, 2009 at 10:41 AM, Mark Breman<breman.mark at gmail.com> wrote:
> Hi,
> I have a timeseries in a csv file that I want to read into an xts object.
> 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-
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>



More information about the R-SIG-Finance mailing list