Converting a data frame to xts

RDataframeCoerceXtsTime Series

R Problem Overview


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!

R Solutions


Solution 1 - R

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.

Solution 2 - R

Well, as.xts assumes by default that the dates are stored in the rownames of the data.frame. Hence the error message. A quick and dirty fix is:

rownames(q) = q[1]
as.xts(q)

But you get an extra column with the dates string. Ideally you would construct the data.frame with the dates as rownames to start with.

Solution 3 - R

Here's a solution using the tidyquant package, which contains a function as_xts() that coerces a data frame to an xts object. It also contains as_tibble() to coerce xts objects to tibbles ("tidy" data frames).

Recreate the data frame (note that the date-time class is used in "tidy" data frames, but any unambiguous date or date time class can be used):

> q
# A tibble: 3 × 2
                    t     x
               <dttm> <dbl>
1 2006-01-01 00:00:00     1
2 2006-01-01 01:00:00     2
3 2006-01-01 02:00:00     3

Use as_xts() to convert to "xts" class. Specify the argument, date_col = t, to designate the "t" column as the dates to use as row names:

> library(tidyquant)
> as_xts(q, date_col = t)
                    x
2006-01-01 00:00:00 1
2006-01-01 01:00:00 2
2006-01-01 02:00:00 3

The return is an xts object with the proper date or date-times as row names.

Solution 4 - R

Here is a posible solution:

library(timetk)
q <- xts::xts(q[,-1], order.by = q$t)

Solution 5 - R

I defined an index with the length equal to the number of rows of my tibble. Only after defining the time sequence separately as shown with the example:

ti= seq(from = ymd_hm("2000-01-01 00:00"),
to = ymd_hm("2000-01-02 01:00"), by =  "30 min", tz = "UTC")

tbl <- tibble(t =ti,
    x = 1:length(t))
)

This code worked:

xts.tbl <- xts(tbl[,-1], order.by = ti)

However all data transformed into characters.

Solution 6 - R

The reason, why it did not work now seems clear, xts does not accept tibbles and even if columns are selected they are still stored as Tibbles. Either the core data may be transformed to matrix ore a vector.The following code works: xls.tbl <- xls(tbl$x, order.by = tbl$t)

Solution 7 - R

Try the following

q$t<-as.xts(q, order.by = as.Date(q$t), dateFormat="POSIXct")

Solution 8 - R

A simple solution is to first convert the data.frame to a data.table:

library(data.table)

qxts <- as.xts(as.data.table(q))

Solution 9 - R

You can simply do the following

qxts <- xts(q[,2],q$t)

Worked for me.

Solution 10 - R

I ran into this as well, but my data date format was slightly different: yyyy-mm-dd as opposed to the OP, which is typical for financial data you download into R.
specifically, as an example: "2022-02-28".
As a result all the suggested solutions do not work.
What works is:

as.xts(q, order.by=as.Date(rownames(q), format = "%Y%m%d"))

assuming your data is in a typical dataframe with dates as rownames (if not, just replace q appropriately with data and date column)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser442446View Question on Stackoverflow
Solution 1 - RDirk EddelbuettelView Answer on Stackoverflow
Solution 2 - RDr GView Answer on Stackoverflow
Solution 3 - RMatt DanchoView Answer on Stackoverflow
Solution 4 - RGregory LopezView Answer on Stackoverflow
Solution 5 - RAloesR2512View Answer on Stackoverflow
Solution 6 - RAloesR2512View Answer on Stackoverflow
Solution 7 - RCricketerView Answer on Stackoverflow
Solution 8 - RJames HirschornView Answer on Stackoverflow
Solution 9 - RBatmanFanView Answer on Stackoverflow
Solution 10 - RtchevrierView Answer on Stackoverflow