0

frame that should be properly formatting with a date index that's properly formatted to be an .xts. However, I cannot complete the conversion to an xts object and receive the following error:

Error in xts(master_zillow5) : 
  order.by requires an appropriate time-based object

Here is my script. I should have the date index created to then make the index:

master_zillow4[,1] <- as.Date(master_zillow4[,1])

master_zillow5 <- master_zillow4 %>% column_to_rownames(., var = 'Date')
test <- xts(master_zillow5,order.by = index(master_zillow5))
index(test) <- mdy(index(test))

Sample of the masterzillow5 object:

structure(list(`77449` = c(112055.39, 112074.39), `77494` = c(221254.91, 
221443.2), `79936` = c(92921.09, 92903.53), `11368` = c(272971.91, 
275503.86), `11385` = c(247208.49, 248297.55), `90011` = c(119682.49, 
120141.32), `60629` = c(85923.22, 85887.92), `77084` = c(111978.96, 
111930.09), `91331` = c(141705.13, 141906.18), `90650` = c(175432.71, 
175560.59), ` 8701` = c(154371.71, 155046.85), `11236` = c(202347.35, 
203171.26), `90201` = c(151762.67, 151854.87), `92335` = c(109520.6, 
110306.38), `11208` = c(169135.26, 169468.6), `10467` = c(151250.38, 
152186.03), `11226` = c(227102.62, 228847.7), `78660` = c(181774.38, 
182131.03), `92503` = c(158099.49, 158438.13), `90250` = c(193337.36, 
193665.76)), row.names = c("2000-01-31", "2000-02-29"), class = "data.frame")

Help is much appreciated!

M--
  • 25,431
  • 8
  • 61
  • 93
js80
  • 385
  • 2
  • 11

1 Answers1

0

Not sure if your approach is going to work using data.frames - I guess column_to_rownames() enforces the row names to be of type character, even if the input was in date format before - since the {tsibble} package allows you to do exactly this, as far as I remember... And there must be a right to exist, right?

This is what ?column_to_rownames tells you about this:

Generally, it is best to avoid row names, because they are basically a character column with different semantics than every other column.

Basically, your date index is not properly defined, apparently.

df <- structure(list(`77449` = c(112055.39, 112074.39), 
                     `77494` = c(221254.91, 221443.2), 
                     `79936` = c(92921.09, 92903.53), 
                     `11368` = c(272971.91, 275503.86), 
                     `11385` = c(247208.49, 248297.55), 
                     `90011` = c(  119682.49, 120141.32), 
                     `60629` = c(85923.22, 85887.92), 
                     `77084` = c(111978.96, 111930.09), 
                     `91331` = c(141705.13, 141906.18), 
                     `90650` = c(175432.71, 175560.59), 
                     ` 8701` = c(154371.71, 155046.85), 
                     `11236` = c(202347.35, 203171.26), 
                     `90201` = c(151762.67, 151854.87), 
                     `92335` = c(109520.6, 110306.38), 
                     `11208` = c(169135.26, 169468.6), 
                     `10467` = c(151250.38, 152186.03), 
                     `11226` = c(227102.62, 228847.7), 
                     `78660` = c(181774.38, 182131.03), 
                     `92503` = c(158099.49, 158438.13), 
                     `90250` = c(193337.36,  193665.76)), 
                row.names = c("2000-01-31", "2000-02-29"), 
                class = "data.frame")

# or was your `index()` coming from a different package?
zoo::index(df)
#> [1] 1 2

This is what you feed to xts::xts(order.by = ...).

# this on the other hand, does work and creates an xts object from your data
xts <- xts::xts(df, order.by = row.names(df) |> as.Date())

class(xts)
#> [1] "xts" "zoo"

xts
#>               77449    77494    79936    11368    11385    90011    60629
#> 2000-01-31 112055.4 221254.9 92921.09 272971.9 247208.5 119682.5 85923.22
#> 2000-02-29 112074.4 221443.2 92903.53 275503.9 248297.5 120141.3 85887.92
#>               77084    91331    90650     8701    11236    90201    92335
#> 2000-01-31 111979.0 141705.1 175432.7 154371.7 202347.4 151762.7 109520.6
#> 2000-02-29 111930.1 141906.2 175560.6 155046.9 203171.3 151854.9 110306.4
#>               11208    10467    11226    78660    92503    90250
#> 2000-01-31 169135.3 151250.4 227102.6 181774.4 158099.5 193337.4
#> 2000-02-29 169468.6 152186.0 228847.7 182131.0 158438.1 193665.8

Created on 2023-06-02 with reprex v2.0.2

dimfalk
  • 853
  • 1
  • 5
  • 15