R xts - 使用 last() 时下标越界
R xts - subscript out of bound while using last()
我在 R 中的 xts 包中使用 last() 函数时发现了一个奇怪的错误。
我有一个 dim 740*1 的 xts 对象,但是 last(data, 1) returns 错误:
> 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)
Error in x[[order(order_by)[n]]] : subscript out of bounds
last(data, 1)
# [,1]
#2017-04-11 5
在上面,dplyr
last
被 xts::last
屏蔽了,所以在这种情况下效果很好。根据加载包的顺序(这里我们在 dplyr
之后加载 xts
),这可能会发生。假设,我们在 xts
之后加载 dplyr
在一个新的 R
会话中,相反的结果为真
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)
Error in x[[order(order_by)[n]]] : subscript out of bounds
这里,选项是使用::
xts::last(data, 1)
# [,1]
#2017-04-11 5
数据
data <- xts(1:5, order.by = Sys.Date()+1:5)
我在 R 中的 xts 包中使用 last() 函数时发现了一个奇怪的错误。
我有一个 dim 740*1 的 xts 对象,但是 last(data, 1) returns 错误:
> 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)
Error in x[[order(order_by)[n]]] : subscript out of bounds
last(data, 1)
# [,1]
#2017-04-11 5
在上面,dplyr
last
被 xts::last
屏蔽了,所以在这种情况下效果很好。根据加载包的顺序(这里我们在 dplyr
之后加载 xts
),这可能会发生。假设,我们在 xts
之后加载 dplyr
在一个新的 R
会话中,相反的结果为真
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)
Error in x[[order(order_by)[n]]] : subscript out of bounds
这里,选项是使用::
xts::last(data, 1)
# [,1]
#2017-04-11 5
数据
data <- xts(1:5, order.by = Sys.Date()+1:5)