1

I am having a problem with the xts package. I am trying to create an xts from a dataframe. For simplicity sake, I tried to replicate what I am trying to do on a small df below:

> df <- japanTOPIX[,1, drop = FALSE]
> typeof(df[,1])
[1] "double"
> typeof(rownames(df))
[1] "character"
> head(df, 3)
           X7164.JT.Equity
12/27/2000            65.0
12/28/2000            66.5
12/29/2000            66.2
> head(as.Date(rownames(df), format = "%m/%d/%Y"),3)
[1] "2000-12-27" "2000-12-28" "2000-12-29"
> timeBased(as.Date(rownames(df), format = "%m/%d/%Y"))
[1] TRUE
> xts(df, by = as.Date(rownames(df), format = "%m/%d/%Y"))
Error in xts(df, by = as.Date(rownames(df), format = "%m/%d/%Y")) : 
  order.by requires an appropriate time-based object
> head(strptime(rownames(df), format = "%m/%d/%Y"),3)
[1] "2000-12-27 EST" "2000-12-28 EST" "2000-12-29 EST"
> timeBased(strptime(rownames(df), format = "%m/%d/%Y"))
[1] TRUE
> xts(df, by = strptime(rownames(df), format = "%m/%d/%Y"))
Error in xts(df, by = strptime(rownames(df), format = "%m/%d/%Y")) : 
  order.by requires an appropriate time-based object
> head(as.POSIXlt(rownames(df), format = "%m/%d/%Y"),3)
[1] "2000-12-27 EST" "2000-12-28 EST" "2000-12-29 EST"
> timeBased(as.POSIXlt(rownames(df), format = "%m/%d/%Y"))
[1] TRUE
> xts(df, by = as.POSIXlt(rownames(df), format = "%m/%d/%Y"))
Error in xts(df, by = as.POSIXlt(rownames(df), format = "%m/%d/%Y")) : 
  order.by requires an appropriate time-based object
> head(as.POSIXct(rownames(df), format = "%m/%d/%Y"),3)
[1] "2000-12-27 EST" "2000-12-28 EST" "2000-12-29 EST"
> timeBased(as.POSIXct(rownames(df), format = "%m/%d/%Y"))
[1] TRUE
> xts(df, by = as.POSIXct(rownames(df), format = "%m/%d/%Y"))
Error in xts(df, by = as.POSIXct(rownames(df), format = "%m/%d/%Y")) : 
  order.by requires an appropriate time-based object

As you can see, I am getting an error in every attempt of trying to create this xts. The package claims I am not ordering by a time based object, but the 'by = ' variable is, in fact, time based. Any help?? (Please let me know if the example or code is not clear, and I will try to clarify.)

EDIT: I thought this may have to do with some non-unique values in the rownames, but that is not the case:

> nrow(df)
[1] 5115
> length(unique(rownames(df)))
[1] 5115
lukehawk
  • 1,423
  • 3
  • 22
  • 48

1 Answers1

2

You are using the xts function incorrectly. The second argument is supposed to be order.by and not by. See the following example with your data:

Data:

df <- read.table(text='
           X7164.JT.Equity
12/27/2000            65.0
12/28/2000            66.5
12/29/2000            66.2')

Solution:

> xts(df, order.by = as.Date(rownames(df), format = "%m/%d/%Y"))
           X7164.JT.Equity
2000-12-27            65.0
2000-12-28            66.5
2000-12-29            66.2

And as you can see it works as intended!

P.S. instead of as.Date you can also use as.POSIXct or as.POSIXlt or strptime. All of them work correctly!

LyzandeR
  • 37,047
  • 12
  • 77
  • 87
  • Wow. Every time. I am an idiot. Thanks for pointing this out! – lukehawk Apr 23 '15 at 22:25
  • Don't worry about it. It has happened to all of us plenty of times :P. We all need some help from time to time debugging. Glad I could be of help :) – LyzandeR Apr 23 '15 at 22:28