Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 1.86 KB  |  hits: 5  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. how to add a missing dates and remove repeated dates in hourly time series
  2. date  Rainfall(mm)
  3. 1970-01-05 00:00:00           1.0
  4. 1970-01-05 01:00:00           1.0
  5. 1970-01-05 05:00:00           3.6
  6. 1970-01-05 06:00:00           3.6
  7. 1970-01-05 07:00:00           2.2
  8. 1970-01-05 08:00:00           2.2
  9. 1970-01-05 09:00:00           2.2
  10. 1970-01-05 10:00:00           2.2
  11. 1970-01-05 11:00:00           2.2
  12. 1970-01-05 13:00:00           2.2
  13. 1970-01-05 13:00:00           2.2
  14. 1970-01-05 13:00:00           2.2
  15.        
  16. # Create a sample data.frame missing every second observation.
  17. df <- data.frame(date=seq.POSIXt(from=as.POSIXct("1970-01-01 00:00:00"), to=as.POSIXct("1970-01-01 10:00:00"), by="2 hours"), rainfall=rnorm(6))
  18. #Create a seq of times without anything missing
  19. grid. <- data.frame(date=seq.POSIXt(as.POSIXct("1970-01-01 00:00:00"), to=as.POSIXct("1970-01-01 10:00:00"), by="1 hours"))
  20. # Merge them together keeping all the values from grid.
  21. dat. <- merge(grid., df, by="date", all.x=TRUE)
  22.        
  23. # The ! means the reverse logic. Therefore TRUE becomes FALSE.
  24. dup_index <- !duplicated(dat.[,1])
  25. # Now re-create the dat. object with only non-duplicated rows.
  26. dat. <- dat.[dup_index,]
  27.        
  28. dat. <- aggregate(dat.[,2], by=list(dat[,1]), FUN=mean)
  29.        
  30. Lines <- "date  Rainfall(mm)
  31. 1970-01-05 00:00:00           1.0
  32. 1970-01-05 01:00:00           1.0
  33. 1970-01-05 05:00:00           3.6
  34. 1970-01-05 06:00:00           3.6
  35. 1970-01-05 07:00:00           2.2
  36. 1970-01-05 08:00:00           2.2
  37. 1970-01-05 09:00:00           2.2
  38. 1970-01-05 10:00:00           2.2
  39. 1970-01-05 11:00:00           2.2
  40. 1970-01-05 13:00:00           2.2
  41. 1970-01-05 13:00:00           2.2
  42. 1970-01-05 13:00:00           2.2"
  43.  
  44. library(zoo)
  45. library(chron)
  46.  
  47. asChron <- function(d, t) as.chron(paste(d, t))
  48. z <- read.zoo(text = Lines, skip = 1, index = 1:2, FUN = asChron, agg = mean)
  49. merge(z, zoo(, seq(start(z), end(z), 1/24))) # as in FAQ