簡體   English   中英

R xts-使用last()時下標超出范圍

[英]R xts - subscript out of bound while using last()

我在R的xts包中使用last()函數時發現了一個奇怪的錯誤。

我有一個昏暗的740 * 1的xts對象,但是last(data,1)返回錯誤:

> tail(data)
            [,1]
2017-02-28 2.092
2017-03-01 2.093
2017-03-02    NA
2017-03-03    NA
2017-03-06    NA
2017-03-07    NA
> dim(data)
[1] 740   1
> last(data,1)
Error in x[[order(order_by)[n]]] : subscript out of bounds

您能幫我理解為什么會這樣嗎?

當從另一個也加載到R會話中的包中以相同的函數名屏蔽該函數時,就會發生這種情況。

dplyr::last(data, 1)

x [[order(order_by)[n]]]中的錯誤:下標超出范圍

last(data, 1)
#         [,1]
#2017-04-11    5

在上面,這是dplyr lastxts::last掩蓋了,因此在這種情況下可以正常工作。 根據加載程序包的順序(此處我們在dplyr之后加載xts )可能會發生。 假設我們在xts之后的新R會話上加載了dplyr ,反之為真

library(xts)
#Loading required package: zoo

#Attaching package: ‘zoo’

#The following objects are masked from ‘package:base’:

#as.Date, as.Date.numeric

library(dplyr)

#Attaching package: ‘dplyr’

#The following objects are masked from ‘package:xts’:

#first, last   ####note this line


data <- xts(1:5, order.by = Sys.Date()+1:5)
last(data, 1)

x [[order(order_by)[n]]]中的錯誤:下標超出范圍

在這里,選項是使用::

xts::last(data, 1)
#           [,1]
#2017-04-11    5

數據

data <- xts(1:5, order.by = Sys.Date()+1:5)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM