【问题标题】:R : create a zoo matrix from a csv fileR:从 csv 文件创建一个动物园矩阵
【发布时间】:2018-07-03 20:50:42
【问题描述】:

所以我想从一个 Csv 文件创建一个动物园矩阵,但它不起作用。我的 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

我从论坛上听了很多解释,但没有任何效果。所以我决定关注这些 instructions。因此,我“创建”了以下代码:

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

【问题讨论】:

    标签: r csv zoo


    【解决方案1】:

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

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

    【讨论】:

    • 这可能是 BOM 问题,或者是第一行开头的另一个不可见字符。在这里查看:stackoverflow.com/questions/42933931/…
    • 嗨!插入代码时没有出现错误消息,但是当我将其插入另一个代码时,我得到了df should be a zoo series with at least one column. Use '[' with 'drop = FALSE
    • 嗨。很抱歉,但我不明白你的评论。您在谈论的新代码中有什么?我们能提供什么帮助?
    • 嗨!问题是我想从我的 csv 文件创建一个动物园矩阵,以便将它用于特定包中的特定功能。但是当使用函数中创建的 csv 矩阵时,我得到了上述错误消息
    • 那请贴出你的函数代码,我们可以测试一下。
    【解决方案2】:

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

    【讨论】:

    • 这在应用于问题中的数据时不会出错。它给出了显示的输出。如果您的意思是您正在尝试以您未解释的方式不同的数据,那么您将需要确定差异是什么。
    猜你喜欢
    • 2017-03-20
    • 2019-02-15
    • 1970-01-01
    • 2012-12-30
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多