为什么read.zoo可以在这个数据帧上工作,但不能在另一个类似的数据帧上工作? (索引有错误的条目)

时间:2014-10-11 15:49:06

标签: r dataframe zoo dplyr

考虑以下脚本:

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

所以,

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

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

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

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

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

编辑:

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 个答案:

答案 0 :(得分:1)

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

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