Character and numeric columns can't both be part of the time series data because the data portion of a zoo object is a matrix (and a matrix must be all numeric, all character or all other type); however, one can split to wide form on the character column using split=
Also we can avoid having to specify function f
by specifying format=
and tz=
. Further, we must specify that a header is present (header=
) and that fields are separated with the "," character (sep=
).
(Below we have used text = Lines
for reproducibliity but in reality replace that with "test.csv"
.)
Lines <- "TICKER,PER,DATE,TIME,CLOSE
SYMBOL,1,20160104,1002,14180.0000000
SYMBOL,1,20160104,1003,14241.0000000"
library(zoo)
read.zoo(text = Lines, header = TRUE, sep = ",", index = c("DATE", "TIME"),
split = "TICKER", format = "%Y%m%d %H%M", tz = "")
giving:
PER CLOSE
2016-01-04 10:02:00 1 14180
2016-01-04 10:03:00 1 14241
Note: If you do want to use your function f
anyways then omit format
and tz
and use:
read.zoo(text = Lines, header = TRUE, sep = ",", index = c("DATE", "TIME"),
split = "TICKER", FUN = f)
This would also work, i.e. read it into a data frame and then read the data frame into a zoo object:
DF <- read.csv(text = Lines) # read.csv defaults to header=TRUE, sep=","
read.zoo(DF, index = c("DATE", "TIME"), split = "TICKER", FUN = f)