Skip to content
Prev 4361 / 15274 Next

how to read in this time series csv file with both dates and times?

There are several things wrong here:

1. FUN= must be a function but its not in the posted code

2. as.chron() uses % codes in its format as described in
?strptime, not the format style for chron().

See R News 4/1 for more on dates and times and also
see the examples in ?read.zoo

Here are two ways to do it.  The first uses as.chron()
and the second defines a custom function, toChron,
that uses chron().

library(zoo)
library(chron)

Lines <- "3/1/2009 23:00:00,123.76,123.94,123.7
3/2/2009 0:00:00,123.85,124.16,123.85
3/2/2009 1:00:00,124.11,124.15,124.06
3/2/2009 2:00:00,124.14,124.32,124.12
3/2/2009 3:00:00,124.2,124.21,124.11
3/2/2009 4:00:00,124.16,124.18,123.94
3/2/2009 5:00:00,124.01,124.2,123.97"

# 1
read.zoo(textConnection(Lines), sep = ",", FUN = as.chron,
	format = "%m/%d/%Y %H:%M:%S")

# 2
toChron <- function(x) chron(sub(" .*", "", x), sub(".* ", "", x))
read.zoo(textConnection(Lines), sep = ",", FUN = toChron)
On Sun, Jun 21, 2009 at 2:42 AM, Michael<comtech.usa at gmail.com> wrote: