On Wed, 12 Aug 2009, Mark Breman wrote:

Hi,
I have a csv file with different datatypes:

2009-01-01, character1, 10, 20.1
2009-01-02, character2, 11, 21.1

(I have attached the file to this post)

I read this file with read.zoo as I want a zoo/xts timeseries:
t = read.zoo("./data.txt", sep=",", dec = ".", header=FALSE)

If I look at the zoo data all integer/numeric columns are read as
character:
str(t)
?zoo? series from 2009-01-01 to 2009-01-02
 Data: chr [1:2, 1:3] " character1" " character2" "10" "11" "20.1" "21.1"
- attr(*, "dimnames")=List of 2
 ..$ : NULL
 ..$ : chr [1:3] "V2" "V3" "V4"
 Index: Class 'Date'  num [1:2] 14245 14246

So I try the colClasses parameter with read.zoo but it looks like this does
not make any difference:
t1 = read.zoo("./data.txt", sep=",", dec = ".", header=FALSE,
colClasses=c("Date", "character", "integer", "numeric"))
str(t1)
?zoo? series from 2009-01-01 to 2009-01-02
 Data: chr [1:2, 1:3] " character1" " character2" "10" "11" "20.1" "21.1"
- attr(*, "dimnames")=List of 2
 ..$ : NULL
 ..$ : chr [1:3] "V2" "V3" "V4"
 Index: Class 'Date'  num [1:2] 14245 14246

Why does read.zoo ignores the colClasses parameter

zoo objects are a matrix plus an "index" (aka time) attribute (of arbitrary class). As the data is a matrix, it can only be of one type, everything numeric typically. Mixed classes would require a data.frame plus index, which is currently not yet supported.

and how do I get integer/numeric data into my zoo series?

You can read it as a data.frame and convert the character column to integers if that makes sense for your data:

  x <- read.table("data.txt", sep = ",")
  x[,2] <- as.numeric(x[,2])
  z <- zoo(as.matrix(x[,-1]), as.Date(as.character(x[,1])))

Alternatively, you could store the data as a data.frame of zoo series (a column in a data.frame can be of class zoo, including factor columns).

hth,
Z

Regards,

-Mark-


______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to