/demo/gsubfn-chron.R

http://gsubfn.googlecode.com/ · R · 22 lines · 12 code · 6 blank · 4 comment · 0 complexity · b02f0d688c7af2f2f012ebca2c3b2f66 MD5 · raw file

  1. # Use read.zoo to read data with a chron time index
  2. # Ignore fractional seconds.
  3. library(zoo)
  4. library(chron)
  5. library(gsubfn)
  6. # test data
  7. Lines <- "2006-01-24 02:41:24.00011,1.22930000,5,1.22950000,7
  8. 2006-01-25 04:41:24.00011,1.22930000,5,1.22950000,7
  9. 2006-01-26 07:41:24.00011,1.22930000,5,1.22950000,7"
  10. # convert to chron
  11. to.chron <- function(x)
  12. strapply(format(x), "([0-9-]+) ([0-9:]+)",
  13. ~ chron(as.numeric(as.Date(dd)), tt), simplify = c)
  14. con <- textConnection(Lines)
  15. read.zoo(con, sep = ",", FUN = to.chron)
  16. close(con)