我有几年的数据,我正在尝试使用动物园对象(Dropbox中的.csv).一旦数据被强制转换为动物园对象,我就会收到错误.我在索引中找不到任何重复的内容.
df <- read.csv(choose.files(default = "", caption = "Select data source", multi = FALSE), na.strings="*")
df <- read.zoo(df, format = "%Y/%m/%d %H:%M", regular = TRUE, row.names = FALSE, col.names = TRUE, index.column = 1)
Warning message:
In zoo(rval3, ix) :
some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique
Run Code Online (Sandbox Code Playgroud)
我试过了:
sum(duplicated(df$NST_DATI))
Run Code Online (Sandbox Code Playgroud)
但结果是0.
谢谢你的帮助!
您使用read.zoo(...)不当.根据文件:
要处理索引,read.zoo以索引作为第一个参数调用FUN.如果未指定FUN,则如果有多个索引列,则将它们粘贴在一起,每个索引列之间有一个空格.使用索引列或粘贴索引列:1.如果指定了tz,则索引列将转换为POSIXct.2.如果指定了format,则索引列将转换为Date.3.否则,启发式尝试在"数字","日期"和"POSIXct"中进行决定.如果指定了format和/或tz,那么它们也会被传递给转换函数.
您正在指定,format=...所以read.zoo(...)将所有内容转换为日期,而不是POSIXct.显然,有许多重复的日期.
简单地说,正确的解决方案是使用:
df <- read.zoo(df, FUN=as.POSIXct, format = "%Y/%m/%d %H:%M")
# Error in read.zoo(df, FUN = as.POSIXct, format = "%Y/%m/%d %H:%M") :
# index has bad entries at data rows: 507 9243 18147 26883 35619 44355
Run Code Online (Sandbox Code Playgroud)
但正如你所看到的,这也不起作用.这里的问题更加微妙.索引是使用POSIXct,但在系统时区(在我的系统上是US Eastern)转换的.引用的行的时间戳与从Standard到DST的转换一致,因此这些时间在US Eastern时区中不存在.如果您使用:
df <- read.zoo(df, FUN=as.POSIXct, format = "%Y/%m/%d %H:%M", tz="UTC")
Run Code Online (Sandbox Code Playgroud)
数据导入正确.
编辑:
正如@ G.Grothendieck指出的那样,这也会起作用,而且更简单:
df <- read.zoo(df, tz="UTC")
Run Code Online (Sandbox Code Playgroud)
您应该设置tz适合数据集的任何timezome.
| 归档时间: |
|
| 查看次数: |
509 次 |
| 最近记录: |