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

Joshua Ulrich josh.m.ulrich at gmail.com
Fri Jul 24 17:36:05 CEST 2009


Mark,

The as.xts(data.frame) case doesn't work because your data.frame does
not have rownames.  as.xts() converts the rownames into the xts index.
 If you read in your data with the following command, you can then
convert the object to xts.

> data <- read.table("US_PMI_Monthly.csv", sep="\t", dec=",", row.names=1)
> head(as.xts(data))
             V2
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.0

I would also note that your data, though in a CSV file, is not in CSV
format.  This may have unforeseen consequences when using commands
written for CSV format files (getSymbols.csv, read.csv, etc.).

HTH,
Josh
--
http://www.fosstrading.com



On Fri, Jul 24, 2009 at 10:07 AM, Mark Breman<breman.mark at gmail.com> wrote:
> Hi Jeff,
> Thanks for explaining it to me.
>
> How about the as.xts(data) call where data is a data.frame type. Is it not
> supposed to work this way?
>
> Regards,
>
> -Mark-
>
> 2009/7/24 Jeff Ryan <jeff.a.ryan at gmail.com>
>
>> Doing nothing wrong, getSymbols.csv isn't general enough to handle this.
>>
>> You can either read in with read.zoo (best option), and then convert
>> to xts if needed, or write/modify your own getSymbols.csv code to read
>> the correct columns.
>>
>> Essentially getSymbols.csv is a simple function to illustrate the
>> generalization facility of getSymbols.  The idea is to write
>> getSymbols.XXXX calls that map to your specific data.
>>
>> That said, a univariate case is sufficiently common to have
>> getSymbols.csv be smart enough to handle.  I will make the changes to
>> do this.
>>
>> Thanks,
>>
>> Jeff
>>
>> On Fri, Jul 24, 2009 at 9: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.
>> >
>>
>>
>>
>> --
>> Jeffrey Ryan
>> jeffrey.ryan at insightalgo.com
>>
>> ia: insight algorithmics
>> www.insightalgo.com
>>
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> 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