0

I have a data that has one date column and 10 other columns. The date column has the format of 199010. so it's yyyymm. It seems like that zoo/xts requires that the date has days info in it. Is there any way to address this issue?

hier ist my data

structure(list(Date = 198901:198905, NoDur = c(5.66, -1.44, 5.51, 
5.68, 5.32)), .Names = c("Date", "NoDur"), class = "data.frame", row.names = c(NA, 
5L))


data<-read.zoo("C:/***/data_port.csv",sep=",",format="%Y%m",header=TRUE,index.column=1,colClasses=c("character",rep("numeric",1)))
purpleblau
  • 589
  • 2
  • 7
  • 18
  • Read this: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – G. Grothendieck Apr 11 '14 at 17:44
  • sorry, what is that? It doesn't seem to answer my question directly. I lost myself while reading it. – purpleblau Apr 11 '14 at 17:47
  • The link does not answer your question. It shows you how to ask a question. The key requirement is that posters must provide a reproducible example. That which means anyone can simplyh copy it to the clipboard from your question and paste it into their R session without additional work and that will run your code and reproduce the problem. – G. Grothendieck Apr 11 '14 at 18:05
  • hope this helps. My original csv data looks like the above table. I can't get it to read because of the Date structure. – purpleblau Apr 11 '14 at 18:09
  • 1
    In this case dput would not be used since the input is supposed to be data to be read into read.zoo. See answer for how you would specify it reproducibly. – G. Grothendieck Apr 11 '14 at 18:52

1 Answers1

2

The code has these problems:

  • the data is space separated but the code specifies that it is comma separated
  • the data does not describe dates since there is no day but the code is using the default of dates
  • the data is not provided in reproducible form. Note how one can simply copy the data and code below and paste it into R without any additional work.

Try this:

Lines <- "Date   NoDur
198901  5.66
198902  -1.44
198903  5.51
198904  5.68
198905  5.32
"

library(zoo)
read.zoo(text = Lines, format = "%Y%m", FUN = as.yearmon, header = TRUE, 
       colClasses = c("character", NA))

The above converts the index to "yearmon" class which probably makes most sense here but it would alternately be possible to convert it to "Date" class by using FUN = function(x, format) as.Date(as.yearmon(x, format)) in place of the FUN argument above.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Thank you so much for all your work! This solved my problem. I guess the proper Date should have year, month and days. Since my data doesn't provide "days" information. It makes a bit hard to read. But your approach is perfectly fine. Thank you again. – purpleblau Apr 11 '14 at 19:14