R:从csv文件创建Zoo矩阵

时间:2018-07-03 20:50:42

标签: r csv zoo

所以我想从一个Csv文件创建一个Zoo矩阵,但是它不起作用。我的csv文件如下:

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 nstructions。因此,我“创建”了以下代码:

> 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 = "")  

我得到:

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

我应该怎么做才能解决这个问题?

谢谢

更新:

好!

> 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)

我得到

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'

2 个答案:

答案 0 :(得分:0)

假设您的csv名为df.csv,请尝试:

library(tidyverse)
library(zoo)

df <- read_csv("df.csv", col_types = cols(Date = col_date(format = "%d/%m/%Y"))) %>%
      read.zoo()

答案 1 :(得分:0)

read.zoo语句存在几个问题:

  • 您无法在Name上拆分数据,因为只有两列,所以没有剩下要拆分的数据。您可以根据需要在“名称”列中读取数据,我们将在下面进行操作。
  • 没有时间,所以Date类就足够了。这样可以避免与时区相关的任何复杂性。 (如果确实需要,可以使用POSIXct,但不建议使用。)
  • format=参数指定错误。

尝试一下

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

给予:

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 

如果您确实想除以Name,我们可以添加一列1用作数据:

DF <- read.csv(text = lines, as.is = TRUE)
DF$data <- 1
read.zoo(DF, format = "%d/%m/%Y", split = "Name")