为什么read.zoo在这个数据帧上工作而在另一个类似的数据帧上没有? (索引有错误的条目)

时间:2021-12-07 22:55:44

Consider the following script:

请考虑以下脚本:

#tmp is some data frame extracted via dplyr:
#       Time    Open
# 2000-01-10 1.26545
# 2000-01-11 1.25107
# 2000-01-12 1.25177
# 2000-01-13 1.26174
# 2000-01-14 1.26354
# 2000-01-15 1.27431
tmp 

#create another data frame that is similar to tmp
dat <- data.frame(Time = paste("2000-01-", 10:15, sep = ""), Open = tmp$Open)

So,

所以,

> #check that all entries in both data frame are equal to each other
> dat == tmp
     Time Open
[1,] TRUE TRUE
[2,] TRUE TRUE
[3,] TRUE TRUE
[4,] TRUE TRUE
[5,] TRUE TRUE
[6,] TRUE TRUE
> #this works
> read.zoo(dat)
2000-01-10 2000-01-11 2000-01-12 2000-01-13 2000-01-14 2000-01-15 
   1.26545    1.25107    1.25177    1.26174    1.26354    1.27431 
> #but this gives an error
> read.zoo(tmp)
Error in read.zoo(tmp) : index has bad entry at data row 1
In addition: Warning message:
drop ignored

Why does read.zoo(tmp) give an error while read.zoo(dat) does not, even though both data frames are essentially the same?

为什么read.zoo(tmp)在read.zoo(dat)没有出错时,即使两个数据帧基本相同?

The only difference between these two data frames are their classes:

这两个数据帧之间的唯一区别是它们的类:

> class(dat)
[1] "data.frame"
> class(tmp)
[1] "tbl_df"     "data.frame"

But the difference shouldn't matter as both are still a data.frame object, right? What is causing the error on read.zoo(tmp) and how do I fix it?

但差异应该不重要,因为两者仍然是data.frame对象,对吧?是什么导致read.zoo(tmp)上的错误,我该如何解决?

EDIT:

编辑:

The output of dput(tmp) is:

dput(tmp)的输出是:

> dput(tmp)
structure(list(Time = structure(1:6, .Label = c("2000-01-10", 
"2000-01-11", "2000-01-12", "2000-01-13", "2000-01-14", "2000-01-15"
), class = "factor"), Open = c(1.26545, 1.25107, 1.25177, 1.26174, 
1.26354, 1.27431)), .Names = c("Time", "Open"), row.names = c(NA, 
-6L), class = c("tbl_df", "data.frame"))

1 个解决方案

#1


1  

It's not clear why zoo can't handle the class tbl_df, per https://github.com/hadley/dplyr/issues/686. Try to coerce using as.data.frame(). This was successful for me:

根据https://github.com/hadley/dplyr/issues/686,目前还不清楚为什么动物园无法处理类tbl_df。尝试强制使用as.data.frame()。这对我来说很成功:

tmp %>% #class "tbl_df" and "data.frame"
as.data.frame %>%
read.zoo

#1


1  

It's not clear why zoo can't handle the class tbl_df, per https://github.com/hadley/dplyr/issues/686. Try to coerce using as.data.frame(). This was successful for me:

根据https://github.com/hadley/dplyr/issues/686,目前还不清楚为什么动物园无法处理类tbl_df。尝试强制使用as.data.frame()。这对我来说很成功:

tmp %>% #class "tbl_df" and "data.frame"
as.data.frame %>%
read.zoo