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