Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a data frame to xts

I'm trying to convert a data frame to xts object using the as.xts()-method. Here is my input dataframe q:

q                       t x   1  2006-01-01 00:00:00  1   2  2006-01-01 01:00:00  2   3  2006-01-01 02:00:00  3  str(q)     'data.frame':   10 obs. of  2 variables:  $ t: POSIXct, format: "2006-01-01 00:00:00" "2006-01-01 01:00:00" "2006-01-01 02:00:00" "2006-01-01 03:00:00" ...    $ x: int  1 2 3 4 5 6 7 8 9 10 

The result is:

> as.xts(q) Error in as.POSIXlt.character(x, tz, ...) :    character string is not in a standard unambiguous format 

This is the simplest example I can think of, so it's quite frustrating not getting it to work... Any help is appreciated!

like image 919
user442446 Avatar asked Nov 28 '10 14:11

user442446


People also ask

How do I convert a Dataframe to a time series object in R?

To convert the given dataframe with the date column to the time series object, the user first needs to import and load the xts package. The user then needs to call the xts() function with the required parameters the main need to call this function is to create the time-series object in R language and at the end use is.

What is XTS format?

eXtensible Time Series (xts) is a powerful package that provides an extensible time series class, enabling uniform handling of many R time series classes by extending zoo.

How do you convert time series in R?

Creating a time seriesThe ts() function will convert a numeric vector into an R time series object. The format is ts(vector, start=, end=, frequency=) where start and end are the times of the first and last observation and frequency is the number of observations per unit time (1=annual, 4=quartly, 12=monthly, etc.).


1 Answers

This is clearly documented --- xts and zoo objects are formed by supplying two arguments, a vector or matrix carrying data and Date, POSIXct, chron, ... type supplying the time information (or in the case of zoo the ordering).

So do something like

 qxts <- xts(q[,-1], order.by=q[,1]) 

and you should be set.

like image 95
Dirk Eddelbuettel Avatar answered Sep 29 '22 18:09

Dirk Eddelbuettel