This vignette gives examples of how to read data in various formats in the zoo package using the read.zoo() function. The function read.zoo() function expects either a text file (or text connection) as input or data frame. The former case is handled by first using read.table() to produce the data frame. (Instead of a text file, the text argument can be used to read a text string that is already stored in R which is used in the examples of this vignette.) Subsequently, read.zoo() provides a wide collection of convenience functionality to turn that data frame into a zoo series with a specific structure and a specific time index. In this vignette, an overview is provided of the wide variety of cases that can be handled with read.zoo(). All examples assume that zoo is already loaded and (if necessary) that the chron package has been loaded as well.
Note that functions read.csv.zoo(), read.csv2.zoo(), read.delim.zoo(), and read.delim2.zoo() are available that call the respective read.*() function instead of read.table() and subsequently read.zoo(). However, these convenience interfaces are not employed in this vignette in order to demonstrate setting all arguments `by hand’.
Example 1
Input class: Text file/connection (space-separated with header).
Input index:integer.
Output class: Multivariate zoo series.
Output index:integer.
Strategy: No transformation of time index needed, hence only a simple call to read.zoo().
Input index:factor with labels indicating AM/PM times but no date.
Output class: Multivariate zoo series.
Output index:times (from chron).
Strategy: The idea is to add some dummy date (here 1970-01-01) to the character lables, then transform to chron and extract the times.
Caveat: The AM/PM labels are locale-dependent (e.g., could be am/pm or a.m./p.m. in other English locales). Hence, it needs to be checked whether the attribute is read correctly in the current locale. Here, a C locale is used to assure reproducibility.
Input class: Text file/connection (space-separated with header).
Input index:factors with labels indicating dates (column 1) and times (column 2).
Output class: Multivariate zoo series.
Output index:chron (from chron).
Strategy: Indicate vector of two columns in index, which is subsequently processed by a FUN taking two arguments and returning a chron time/date.
Lines<-"Date Time O H L C1/2/2005 17:05 1.3546 1.3553 1.3546 1.354951/2/2005 17:10 1.3553 1.3556 1.3549 1.355251/2/2005 17:15 1.3556 1.35565 1.35515 1.35531/2/2005 17:25 1.355 1.3556 1.355 1.35551/2/2005 17:30 1.3556 1.3564 1.35535 1.3563"f<-function(d, t)as.chron(paste(as.Date(chron(d)), t))z<-read.zoo(text =Lines, header =TRUE, index =1:2, FUN =f)z
O H L C
(01/02/05 17:05:00) 1.3546 1.35530 1.35460 1.35495
(01/02/05 17:10:00) 1.3553 1.35560 1.35490 1.35525
(01/02/05 17:15:00) 1.3556 1.35565 1.35515 1.35530
(01/02/05 17:25:00) 1.3550 1.35560 1.35500 1.35550
(01/02/05 17:30:00) 1.3556 1.35640 1.35535 1.35630
Example 5
Input class: Text file/connection (space-separated with non-matching header).
Input index:factors with labels indicating dates (column 6) and unneeded weekdays (column 5) and times (column 7).
Output class: Multivariate zoo series.
Output index:Date.
Strategy: First, skip the header line, remove unneeded columns by setting colClasses to "NULL", and set suitable col.names. Second, convert the date column to a Date index using format. Finally, aggregate over duplicate dates, keeping only the last observation.
views number
2009-07-09 1011144 1247135553
2009-07-17 1083059 1247845824
Alternative approach: Above approach labels each point as it was originally labeled, i.e., if Thursday is used it gets the date of that Thursday. Another approach is to always label the resulting point as Friday and also use the last available value even if its not Thursday.
Create daily grid and fill in so Friday is filled in with prior value if Friday is NA.
g<-seq(start(z), end(z), by ="day")z.filled<-na.locf(z, xout =g)
Extract Fridays, including those filled in from previous day.
Input class: Text file/connection (space-separated without header).
Input index:factor with labels indicating dates.
Output class: Multivariate zoo series, with separate columns depending on column 2.
Output index:Date.
Strategy: Non-standard na.strings format needs to be specified, series is split based on second column, and date format (in column 1, default) needs to be specified.
Lines<-"13/10/2010 A 2313/10/2010 B 1213/10/2010 C 12414/10/2010 A 4314/10/2010 B 5414/10/2010 C 6515/10/2010 A 4315/10/2010 B N.A.15/10/2010 C 65"z<-read.zoo(text =Lines, na.strings ="N.A.", format ="%d/%m/%Y", split =2)z
A B C
2010-10-13 23 12 124
2010-10-14 43 54 65
2010-10-15 43 NA 65
Example 9
Input class: Text file/connection (comma-separated with header).
Input index:factor with labels indicating date/time.
Output class: Univariate zoo series.
Output index:chron (from chron) or POSIXct.
Strategy: Ignore first two columns by setting colClasses to "NULL". Either produce chron index via as.chron() or use all defaults to produce POSIXct by setting tz.
Input class: Text file/connection (space-separated with non-matching header).
Input index:factor with labels indicating date (column 3) and time (column 4).
Output class: Multivariate zoo series.
Output index:chron (from chron) or POSIXct.
Strategy:skip non-matching header and extract date/time from two columns index = 3:4. Either using sequence of two functions FUN and FUN2 or employ defaults yielding POSIXct.
Strategy: Given a data.frame only keep last row in each month. Use read.zoo() to convert to zoo and then na.locf() and duplicated().
DF<-structure(list( Date =structure(c(14609, 14638, 14640, 14666, 14668, 14699,14729, 14757, 14759, 14760), class ="Date"), A =c(4.9, 5.1, 5, 4.8, 4.7, 5.3, 5.2, 5.4, NA, 4.6), B =c(18.4, 17.7, NA, NA, 18.3, 19.4, 19.7, NA, NA, 18.1), C =c(32.6, NA, 32.8, NA, 33.7, 32.4, 33.6, NA, 34.5, NA), D =c(77, NA, 78.7, NA, 79, 77.8, 79, 81.7, NA, NA)), names =c("Date", "A", "B", "C", "D"), row.names =c(NA, -10L), class ="data.frame")DF
Date A B C D
1 2009-12-31 4.9 18.4 32.6 77.0
2 2010-01-29 5.1 17.7 NA NA
3 2010-01-31 5.0 NA 32.8 78.7
4 2010-02-26 4.8 NA NA NA
5 2010-02-28 4.7 18.3 33.7 79.0
6 2010-03-31 5.3 19.4 32.4 77.8
7 2010-04-30 5.2 19.7 33.6 79.0
8 2010-05-28 5.4 NA NA 81.7
9 2010-05-30 NA NA 34.5 NA
10 2010-05-31 4.6 18.1 NA NA
Input class: Text file/connection (comma-separated with header).
Input index:factor with labels indicating date/time.
Output class: Multivariate zoo series.
Output index:POSIXct or chron (from chron).
Strategy: Dates and times are in standard format, hence the default POSIXct can be produced by setting tz or, alternatively, chron can be produced by setting as.chron() as FUN.
Input class: Text file/connection (space-separated with header).
Input index:numeric year with quarters represented by separate columns.
Output class: Univariate zoo series.
Output index:yearqtr.
Strategy: First, create a multivariate annual time series using the year index. Then, create a regular univariate quarterly series by collapsing the annual series to a vector and adding a new yearqtr index from scratch.