【问题标题】:Create XTS with dates with different number of rows使用具有不同行数的日期创建 XTS
【发布时间】:2016-12-19 13:07:15
【问题描述】:

尝试创建 xts 文件,但在加载后格式化后,日期的行数与数据的行数不同。我的数据有许多列,行数不等,从 20 到 200 不等。我想在加载后创建一个单独的变量,变量取决于我想查看的组合,所以我想要一个完整的 data.frame在创建变量之前使用 NA,我将在其中 na.omit 并减小尺寸。

代码如下:

#load file with desired composite
allcomposites <- read.csv("Composites 2014.08.31.csv", header = T)
compositebench <- allcomposites[1, 2:ncol(allcomposites)]
dates1 <- as.Date(allcomposites$Name, format = "%m/%d/%Y")
allcomposites <- as.data.frame(lapply(allcomposites[2:nrow(allcomposites),2:ncol(allcomposites)],    as.numeric))
allcomposites <- as.xts(allcomposites, order.by = dates1)
## Error in xts(x, order.by = order.by, frequency = frequency, ...) :
##     NROW(x) must match length(order.by)

编辑以显示所有复合材料的外观:

Name    Composite1  Composite2  Composite3  Composite4 Composite5
Bmark   229 229 982 612 995
8/31/2014   0.9979  0.9404  4.3808      3.9296
7/31/2014   -0.4563 -0.3038 -1.7817     -1.7248
6/30/2014   0.205   0.2234  2.2184      2.7304
5/31/2014   1.311   1.5771  3.4824      1.7601
4/30/2014   0.9096  1.0187  -1.9195     1.2964

【问题讨论】:

  • 你能举一个小例子来说明所有复合材料的样子吗?
  • 一个问题是 Bmark 被 allcomposites$Name 捕获,而您尝试将 Bmark 转换为日期。
  • 正如@DMT 所说:您需要省略dates1 的第一个观察结果:dates1 &lt;- as.Date(allcomposites$Name[-1], format = "%m/%d/%Y")

标签: r xts


【解决方案1】:

dates1allcomposites 中删除第一行时需要更加小心。

这是实现目标的另一种方法:

Lines <- "Name    Composite1  Composite2  Composite3  Composite4 Composite5
Bmark   229 229 982 612 995
8/31/2014   0.9979  0.9404  4.3808      3.9296
7/31/2014   -0.4563 -0.3038 -1.7817     -1.7248
6/30/2014   0.205   0.2234  2.2184      2.7304
5/31/2014   1.311   1.5771  3.4824      1.7601
4/30/2014   0.9096  1.0187  -1.9195     1.2964"

library(xts)
# use fill=TRUE because you only provided data for 4 composites
allcomp <- read.table(text=Lines, header=TRUE, fill=TRUE)
# remove the first row that contains "Bmark"
allcomp <- allcomp[-1,]
# create an xts object from the remaining data
allcomp_xts <- xts(allcomp[,-1], as.Date(allcomp[,1], "%m/%d/%Y"))

【讨论】:

    【解决方案2】:
    ## Error in xts(x, order.by = order.by, frequency = frequency, ...
    ##     NROW(x) must match length(order.by)
    

    我浪费了几个小时遇到这个错误。无论我是否遇到完全相同的问题,我都会展示我是如何解决此错误消息的,以防它为您省去我的痛苦。

    我通过几个导入函数导入了一个 Excel 或 CSV 文件(两者都尝试过),然后尝试将我的数据(作为 data.frame 或 .zoo 对象)转换为 xts 对象并不断收到错误,包括这个。

    我尝试单独创建一个日期向量以作为 order.by 参数传入。我尝试确保 data.frame 的行的日期向量是相同的。有时有效,有时无效,原因我无法解释。即使它确实有效,R 也将我所有的数字数据“强制”为字符数据。 (后来给我带来了无穷无尽的问题。注意强制,我学会了。)

    这些错误一直发生,直到:

    对于 xts 转换,我使用导入的 Excel 表中的日期列作为带有 as.Date() 修饰符的 order.by 参数,并且我*删除了日期列 /em> 转换成 xts.*

    这是工作代码:

    xl_sheet <- read_excel("../path/to/my_excel_file.xlsx")
    sheet_xts <- xts(xl_sheet[-1], order.by = as.Date(xl_sheet$date))
    

    注意我的日期列是第一列,所以 xl_sheet[-1] 删除了第一列。

    【讨论】:

      猜你喜欢
      • 2022-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多