[R] Converting to XTS loses data.frame structure

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Tue May 29 21:34:02 CEST 2012


Only convert numeric columns to time series. This is a fundamental limitation of ts, xts, and zoo.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Noah Silverman <noahsilverman at ucla.edu> wrote:

>Hello,
>
>I noticed something odd when working with data frames and xts objects.
>
>If I read in a CSV file, R creates a nice data.frame.  This works well.
>
>If I then convert to an XTS object, I see that all the values in the
>data are now quoted.  My data is a mix of numeric and character.  This
>is usually seen when converting a data.frame to a matrix, as R will
>treat all the data as the same class. (character)
>
>How can I ensure that R creates an XTS object that is still a
>data.frame so that all the data are the correct type??
>
>
>In the example below, you can see how as.xts() creates an object with
>the correct date and time index.  Please notice how all the values are
>now quoted indicating that R considers them a string.
>
>thee data read in "d" is a data.frame:
>> str(d)
>'data.frame':	248 obs. of  4 variables:
>
>However, converting to an XTS seems to break that.
>Example:
>
>=============================================
>d <- (read.csv(file.path(dataDir,thisFile), as.is=T)) 
>myXTS <- as.xts( d, order.by=as.POSIXct(strptime(paste(d$Date, d$Time),
>'%m/%d/%y %H:%M'))) 
>
>head(d)
>  Date         Obsever    Val.1        Time
>1 10/12/09      PL        15           12:44
>2 10/12/09      PL        15           12:44
>
>head(myXTS)
>                    Date       Obsever Val.1      Time
>2009-10-12 12:44:00 "10/12/09" "PL"    "15"      "12:44"        
>2009-10-12 12:44:00 "10/12/09" "PL"    "15"      "12:44"  
>=============================================
>
>
>--
>Noah Silverman
>UCLA Department of Statistics
>8208 Math Sciences Building
>Los Angeles, CA 90095
>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list