简体   繁体   中英

Select a value from time series by date in R

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:

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

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.

This code in R gives you the result you expect.

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

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(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 :

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 .

Looking for a simple command, one line and no library needed? You might try this.

as.numeric(window(producers.price, 2015.1, 2015.2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM