0

So I would like to create a zoo matrix from a Csv file but it doesn't work. My csv file is such as :

Date,Name
13/02/2015,Austria
07/08/2015,Austria
05/02/2016,Austria
22/07/2016,Austria
05/08/2016,Austria
03/02/2017,Austria
28/07/2017,Austria
26/01/2018,Austria
25/07/2011,Austria
28/10/2011,Austria
25/11/2011,Austria
20/01/2012,Austria
24/02/2012,Austria
26/04/2012,Austria
25/05/2012,Austria
11/07/2012,Austria
17/08/2012,Austria
09/11/2012,Austria
25/04/2013,Austria
29/07/2013,Austria
27/09/2013,Austria
30/09/2013,Austria
23/10/2013,Austria
16/01/2014,Austria
30/01/2014,Austria
21/02/2014,Austria
27/05/2014,Austria
30/07/2014,Austria
15/08/2014,Austria

I've followed many explanations from the forum but nothing works. So I've decided to follow these instructions. Thus, I've "created" the following code :

> Lines <- "Date,Name
+ 13/02/2015,Austria
+ 07/08/2015,Austria
+ 05/02/2016,Austria
+ 22/07/2016,Austria
+ 05/08/2016,Austria
+ 03/02/2017,Austria
+ 28/07/2017,Austria
+ 26/01/2018,Austria
+ 25/07/2011,Austria
+ 28/10/2011,Austria
+ 25/11/2011,Austria
+ 20/01/2012,Austria
+ 24/02/2012,Austria
+ 26/04/2012,Austria
+ 25/05/2012,Austria
+ 11/07/2012,Austria
+ 17/08/2012,Austria
+ 09/11/2012,Austria
+ 25/04/2013,Austria
+ 29/07/2013,Austria
+ 27/09/2013,Austria
+ 30/09/2013,Austria
+ 23/10/2013,Austria
+ 16/01/2014,Austria
+ 30/01/2014,Austria
+ 21/02/2014,Austria
+ 27/05/2014,Austria
+ 30/07/2014,Austria
+ 15/08/2014,Austria
+ "
> library(zoo)
> read.zoo(text = Lines, header = TRUE, sep = ",", index = c("Date"), split = "Name", format = "d%m%Y", tz = "")  

and I get :

Error in read.zoo(text = Lines, header = TRUE, sep = ",", index = c("Date"),  : 
  index has bad entries at data rows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

What should I do, to resolve this ?

Thanks

Update:

Ok!

> df <- read_csv("austriatestfitch.csv", col_types = cols(When = col_date(format = "%d/%m/%Y"))) %>%
+     read.zoo()
> auss1 = df
> str(auss1)
> aus2 = read.csv(file.choose(), header = TRUE)
> str(aus2)
library(eventstudy)
> eventstudy(firm.returns = auss1,
+            event.list = aus2,
+            event.window = 10,
+            is.levels =  FALSE,
+            type = "None",
+            to.remap = TRUE,
+            remap = "cumsum",
+            inference = TRUE,
+            inference.strategy = "bootstrap",
+            model.args = NULL)

and I obtain

Error in eventstudy(firm.returns = auss1, event.list = aus2, event.window = 10,  : 
  firm.returns should be a zoo series with at least one column. Use '[' with 'drop = FALSE'
bixoez
  • 27
  • 4

2 Answers2

0

Assuming that your csv is called df.csv, try:

library(tidyverse)
library(zoo)

df <- read_csv("df.csv", col_types = cols(Date = col_date(format = "%d/%m/%Y"))) %>%
      read.zoo()
Nicolás Velasquez
  • 5,623
  • 11
  • 22
  • It could be a BOM issue, or another invisible cahracter at the start of your first line. Check here: https://stackoverflow.com/questions/42933931/parser-does-not-match-column-name-in-csv-file-when-importing-using-readr-packag – Nicolás Velasquez Jul 03 '18 at 22:02
  • Hi! No error message appears when I insert your code, but when I insert it in another code I got `df should be a zoo series with at least one column. Use '[' with 'drop = FALSE` – bixoez Jul 03 '18 at 22:12
  • Hi. I am sorry but I do not understand your comment. What is in the new code you are talking about? How can we help? – Nicolás Velasquez Jul 03 '18 at 22:50
  • Hi ! The thing is that I want to create a zoo matrix from my csv file, in order to use it for a specific function from a specific package. But when using the csv matrix created in the function i get the above error message – bixoez Jul 03 '18 at 22:54
  • Then please post the code of your function, so we can test it. – Nicolás Velasquez Jul 03 '18 at 22:55
  • Ok ! I've updated my question as the code was too long to be copied here :) – bixoez Jul 03 '18 at 23:08
0

There are several problems with the read.zoo statement:

  • you can't split the data on Name as there are only two columns so that leaves no data left to split. You can read in the Name column as data if you want and we do that below.
  • there are no times so Date class would be sufficient. That would avoid any complexities associated with time zones. (You can use POSIXct if you really want but it is not recommended.)
  • the format= argument is incorrectly specified.

Try this

read.csv.zoo(text = Lines, format = "%d/%m/%Y")

giving:

2011-07-25 2011-10-28 2011-11-25 2012-01-20 2012-02-24 2012-04-26 2012-05-25 
   Austria    Austria    Austria    Austria    Austria    Austria    Austria 
2012-07-11 2012-08-17 2012-11-09 2013-04-25 2013-07-29 2013-09-27 2013-09-30 
   Austria    Austria    Austria    Austria    Austria    Austria    Austria 
2013-10-23 2014-01-16 2014-01-30 2014-02-21 2014-05-27 2014-07-30 2014-08-15 
   Austria    Austria    Austria    Austria    Austria    Austria    Austria 
2015-02-13 2015-08-07 2016-02-05 2016-07-22 2016-08-05 2017-02-03 2017-07-28 
   Austria    Austria    Austria    Austria    Austria    Austria    Austria 
2018-01-26 
   Austria 

We could add a column of 1's, say, to use as data if you really do want to split by Name:

DF <- read.csv(text = lines, as.is = TRUE)
DF$data <- 1
read.zoo(DF, format = "%d/%m/%Y", split = "Name")
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • This does not give an error when applied to the data in the question. It gives the output shown. If what you mean is you are trying it on data which differs in a way that you haven't explained then you will need to determine what the difference is. – G. Grothendieck Jul 04 '18 at 17:55