Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug in R align.time/aggregate?

I have a time series x:

dput(x)
structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28.8, 0, 0, 
0, 0, 0, 0, 0, 0), index = structure(c(1193524740, 1193525340, 
1193525940, 1193526540, 1193527140, 1193527740, 1193528340, 1193528940, 
1193529540, 1193530140, 1193530740, 1193531340, 1193531940, 1193532540, 
1193533140, 1193533740, 1193534340, 1193534940, 1193535540, 1193536140, 
1193536740, 1193537340), class = c("POSIXct", "POSIXt")), class = "zoo")

I aggregate to 1 hour

as.xts(aggregate(x, align.time(index(x), 60*60))) 
                    [,1]
2007-10-28 00:00:00  0.0
2007-10-28 01:00:00  0.0
2007-10-28 01:00:00 28.8
2007-10-28 02:00:00  0.0
2007-10-28 03:00:00  0.0

As you can see, I get "2007-10-28 01:00:00" duplicated.

I wonder whether this is a bug or I'm doing something wrong?

like image 417
Claudia Avatar asked Mar 26 '14 20:03

Claudia


1 Answers

More than likely this is not bug and is a matter of your locale...

library(xts)

## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric

library(zoo)
v <- rep(0,22)
v[14] <- 28.8

d <- c(1193524740, 1193525340, 1193525940, 1193526540, 1193527140, 1193527740, 1193528340, 1193528940, 1193529540, 1193530140, 1193530740, 1193531340, 1193531940, 1193532540, 1193533140, 1193533740, 1193534340, 1193534940, 1193535540, 1193536140, 1193536740, 1193537340)
orig <- "1970-01-01 00:00.00 UTC"

# Output with duplicate...
x <- zoo(v, as.POSIXct( d, tz="Europe/London", origin=orig) )
as.xts(aggregate(x, align.time(index(x), 60*60)))

##                     [,1]
## 2007-10-28 00:00:00  0.0
## 2007-10-28 01:00:00  0.0
## 2007-10-28 01:00:00 28.8
## 2007-10-28 02:00:00  0.0
## 2007-10-28 03:00:00  0.0

# Output with duplicate...
x <- zoo(v, as.POSIXct( d, tz="UTC", origin = orig) )
as.xts(aggregate(x, align.time(index(x), 60*60)))

##                     [,1]
## 2007-10-27 23:00:00  0.0
## 2007-10-28 00:00:00  0.0
## 2007-10-28 01:00:00 28.8
## 2007-10-28 02:00:00  0.0
## 2007-10-28 03:00:00  0.0

See ?Sys.timezone and the SO info page for the timezone tag; particularly the Olson Time Zone Database information which R uses, and the summary of answers to the Daylight saving time and time zone best practices quetion.

like image 70
Thell Avatar answered Oct 10 '22 00:10

Thell