How to select a value from time series corresponding needed date?
如何从对应所需日期的时间序列中选择一个值?
I create a monthly time series object with command:
我用命令创建一个月度时间序列对象:
producers.price <- ts(producers.price, start=2012+0/12, frequency=12)
Then I try to do next:
然后我尝试做下一个:
value <- producers.price[as.Date("01.2015", "%m.%Y")]
But this doesn't make that I want and value is equal
但这并不是我想要的,价值是平等的
[1] NA
Instead of 10396.8212805739
if producers.price is:
如果producer.price是:而不是10396.8212805739
producers.price <- structure(c(7481.52109434237, 6393.18959031561, 6416.63065650718,
5672.08354710121, 7606.24186413516, 5201.59247092013, 6488.18361474813,
8376.39182893415, 9199.50916585545, 8261.87133079494, 8293.8195347453,
8233.13630279516, 7883.17272003961, 7537.21001580393, 6566.60260432381,
7119.99345843556, 8086.40101607729, 9125.11104610046, 10134.0228610828,
10834.5732454454, 9410.35031874371, 9559.36933274129, 9952.38679679724,
10390.3628690951, 11134.8432864557, 11652.0075507499, 12626.9616107684,
12140.6698452193, 11336.8315981684, 10526.0309052316, 10632.1492109584,
8341.26367412737, 9338.95688558448, 9732.80173656971, 10724.5525831506,
11272.2273444623, 10396.8212805739, 10626.8428853062, 11701.0802817581,
NA), .Tsp = c(2012, 2015.25, 12), class = "ts")
2 个解决方案
#1
1
So, I had/have a similar problem and was looking all over to solve it. My solution is not as great as I'd have wanted it to be, but it works. I tried it out with your data and it seems to give the right result.
所以,我有/有一个类似的问题,并期待着解决它。我的解决方案并不像我想要的那样好,但它确实有效。我用你的数据试了一下,似乎给出了正确的结果。
Explanation
说明
Turns out in R time series data is really stored as a sequence, starting at 1, and not with yout T. Eg. If you have a time series that starts in 1950 and ends in 1960 with each data at one year interval, the Y at 1950 will be ts[1] and Y at 1960 will be ts[11]. Based on this logic you will need to subtract the date from the start of the data and add 1 to get the value at that point.
事实上R时间序列中的数据实际上是作为一个序列存储的,从1开始,而不是你的T.如果你有一个时间序列,从1950年开始到1960年结束,每个数据以一年为间隔,1950年的Y将是ts [1],而1960年的Y将是ts [11]。基于此逻辑,您需要从数据开头减去日期,并添加1以获取该点的值。
This code in R gives you the result you expect.
R中的此代码为您提供了您期望的结果。
producers.price[((as.yearmon("2015-01")- as.yearmon("2012-01"))*12)+1]
If you need help in the time calculations, check this answer You will need the zoo
and lubridate
packages Get the difference between dates in terms of weeks, months, quarters, and years
如果您在时间计算中需要帮助,请检查此答案您将需要动物园和润滑剂包以星期,月,季度和年为单位获取日期之间的差异
Hope it helps :)
希望能帮助到你 :)
#2
0
1) window.ts
1)window.ts
The window.ts
function is used to subset a "ts"
time series by a time window. The window command produces a time series with one data point and the [[1]] makes it a straight numeric value:
window.ts函数用于按时间窗口对“ts”时间序列进行子集化。 window命令产生一个带有一个数据点的时间序列,[[1]]使它成为一个直的数值:
window(producers.price, start = 2015 + 0/12, end = 2015 + 0/12)[[1]]
## [1] 10396.82
2) zoo We can alternately convert it to zoo and subscript it by a yearmon class variable and then use [[1]] or coredata
to convert it to a plain number or we can use window.zoo
much as we did with window.ts
:
2)动物园我们可以交替地将它转换为动物园并通过一个年级类变量下标它然后使用[[1]]或coredata将其转换为普通数字,或者我们可以像window.ts一样使用window.zoo。 :
library(zoo)
as.zoo(producers.price)[as.yearmon("2015-01")][[1]]
## [1] 10396.82
coredata(as.zoo(producers.price)[as.yearmon("2015-01")])
## [1] 10396.82
window(as.zoo(producers.price), 2015 + 0/12 )[[1]]
## [1] 10396.82
coredata(window(as.zoo(producers.price), 2015 + 0/12 ))
## [1] 10396.82
3) xts The four lines in (2) also work if library(zoo)
is replaced with library(xts)
and as.zoo
is replaced with as.xts
.
3)xts如果库(zoo)被库(xts)替换并且as.zoo被替换为as.xts,则(2)中的四行也起作用。
#1
1
So, I had/have a similar problem and was looking all over to solve it. My solution is not as great as I'd have wanted it to be, but it works. I tried it out with your data and it seems to give the right result.
所以,我有/有一个类似的问题,并期待着解决它。我的解决方案并不像我想要的那样好,但它确实有效。我用你的数据试了一下,似乎给出了正确的结果。
Explanation
说明
Turns out in R time series data is really stored as a sequence, starting at 1, and not with yout T. Eg. If you have a time series that starts in 1950 and ends in 1960 with each data at one year interval, the Y at 1950 will be ts[1] and Y at 1960 will be ts[11]. Based on this logic you will need to subtract the date from the start of the data and add 1 to get the value at that point.
事实上R时间序列中的数据实际上是作为一个序列存储的,从1开始,而不是你的T.如果你有一个时间序列,从1950年开始到1960年结束,每个数据以一年为间隔,1950年的Y将是ts [1],而1960年的Y将是ts [11]。基于此逻辑,您需要从数据开头减去日期,并添加1以获取该点的值。
This code in R gives you the result you expect.
R中的此代码为您提供了您期望的结果。
producers.price[((as.yearmon("2015-01")- as.yearmon("2012-01"))*12)+1]
If you need help in the time calculations, check this answer You will need the zoo
and lubridate
packages Get the difference between dates in terms of weeks, months, quarters, and years
如果您在时间计算中需要帮助,请检查此答案您将需要动物园和润滑剂包以星期,月,季度和年为单位获取日期之间的差异
Hope it helps :)
希望能帮助到你 :)
#2
0
1) window.ts
1)window.ts
The window.ts
function is used to subset a "ts"
time series by a time window. The window command produces a time series with one data point and the [[1]] makes it a straight numeric value:
window.ts函数用于按时间窗口对“ts”时间序列进行子集化。 window命令产生一个带有一个数据点的时间序列,[[1]]使它成为一个直的数值:
window(producers.price, start = 2015 + 0/12, end = 2015 + 0/12)[[1]]
## [1] 10396.82
2) zoo We can alternately convert it to zoo and subscript it by a yearmon class variable and then use [[1]] or coredata
to convert it to a plain number or we can use window.zoo
much as we did with window.ts
:
2)动物园我们可以交替地将它转换为动物园并通过一个年级类变量下标它然后使用[[1]]或coredata将其转换为普通数字,或者我们可以像window.ts一样使用window.zoo。 :
library(zoo)
as.zoo(producers.price)[as.yearmon("2015-01")][[1]]
## [1] 10396.82
coredata(as.zoo(producers.price)[as.yearmon("2015-01")])
## [1] 10396.82
window(as.zoo(producers.price), 2015 + 0/12 )[[1]]
## [1] 10396.82
coredata(window(as.zoo(producers.price), 2015 + 0/12 ))
## [1] 10396.82
3) xts The four lines in (2) also work if library(zoo)
is replaced with library(xts)
and as.zoo
is replaced with as.xts
.
3)xts如果库(zoo)被库(xts)替换并且as.zoo被替换为as.xts,则(2)中的四行也起作用。