[R] read.zoo problems

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jan 14 05:14:40 CET 2012


On Fri, Jan 13, 2012 at 5:14 PM, KTD Services <ktdservices01 at gmail.com> wrote:
> I am having trouble importing a zoo object after writing it.  When I try to
> import using read.zoo, I get the following error:
>
> Error in read.zoo(data_location) : index contains NAs
>
> But when I inspect the file I don't have any NAs
>
> A sample data (.txt 2KB) can be downloaded at:
> http://www.mediafire.com/?29dqndxf2do9m9e
>

The problem is that since there is a space between the date and time
and you are also using a space to separate fields the file has become
inconsistent.

It would be better to write it out as a csv file so that the date and
time remain as a single field.

If its too late for that then you can still recover it but its a bit
more work.  First get the column names and then read everything but
the header adding the column names yourself like this:

library(zoo)
File <- "test2.txt"
cn <- c("Date", read.table(File, nrow = 1, as.is = TRUE))
z <- read.zoo(File, header = FALSE, skip = 1, index = 1:2, tz = "",
col.names = cn)

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list