无法从具有正确格式的日期索引的 Dataframe 创建xts对象

w8f9ii69  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(83)

应该使用正确格式化为. xts的日期索引正确格式化的帧。但是,我无法完成到xts对象的转换,并收到以下错误:

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

这是我的剧本。我应该创建日期索引,然后创建索引:

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))

masterzillow 5对象的示例:

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")

非常感谢帮助!

ubby3x7f

ubby3x7f1#

我不确定你的方法是否适用于data.frames -我猜column_to_rownames()强制行名称为字符类型,即使输入之前是日期格式-因为{tsibble}包允许你这样做,据我所知...必须有生存的权利,对吧?
下面是?column_to_rownames告诉你的:
一般来说,最好避免行名称,因为它们基本上是一个字符列,与其他列具有不同的语义。
基本上,你的日期索引没有正确定义,显然。

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

这就是您提供给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

创建于2023-06-02使用reprex v2.0.2

相关问题