R chron%在%比较中仅识别每秒一次的日期
我在R中使用zoo和chron包来读取和转换数据。在某一点上,我需要选择与另一个chron对象相对应的chron索引zoo对象的一部分。不幸的是,使用%in%运算符只能得到部分相应日期。以下是重现错误的MWE:R chron%在%比较中仅识别每秒一次的日期,r,date,zoo,chron,R,Date,Zoo,Chron,我在R中使用zoo和chron包来读取和转换数据。在某一点上,我需要选择与另一个chron对象相对应的chron索引zoo对象的一部分。不幸的是,使用%in%运算符只能得到部分相应日期。以下是重现错误的MWE: library(chron) library(zoo) chron1 <- seq(chron("2013-01-01","00:00:00", format=c(dates="y-m-d",times="h:m:s")), chron("2013-01
library(chron)
library(zoo)
chron1 <- seq(chron("2013-01-01","00:00:00", format=c(dates="y-m-d",times="h:m:s")),
chron("2013-01-01","03:10:00", format=c(dates="y-m-d",times="h:m:s")),by=1./1440.)
x1 <- runif(200)
z1 <- zoo(x1,chron1)
chron10 <- trunc(chron1, "00:10:00")
x10 <- aggregate(z1,chron10,FUN=sum)
which(index(x10) %in% chron1)
chron对象是浮点型的,因此根据计算方式,在相同的日期时间上可能会有细微的差异<代码>格式并比较它们:
which(format(index(x10)) %in% format(chron1))
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
这也适用于trunc
使用eps
值,以确保略少于一分钟的输入不会再被截断一分钟。请参见《真实时间》
which(trunc(index(x10), "minutes") %in% trunc(chron1, "minutes"))
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
另请参见它不是
chron1a
,而是chron1
。请编辑问题。这是一个舍入错误问题,index(x10)[1]-chron1[11]
输出[1]-0.006944444
。以下内容似乎解决了这个问题:round(索引(x10))%in%round(chron1)
@RuiBarradas,发布为答案?@RuiBarradas:谢谢,编辑了这个问题。”“轮”不幸的是,轮到了全天。我不确定它能用几分钟。
which(trunc(index(x10), "minutes") %in% trunc(chron1, "minutes"))
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20