简体   繁体   中英

How to average 2 time series' in R?

Suppose I have 2 different time series' in R.

1)

Jan 18------Feb 18------Mar 18------Apr 18

-----1------------2--------------4-------------10

And 2)

Jan 18------Feb 18------Mar 18------Apr 18

-----4------------6--------------10-------------4

How can I generate the time series based on the average of the 2?

Ie

Jan 18------Feb 18------Mar 18------Apr 18

-----2.5------------4--------------7-------------7

My thought was to somehow extract the values into a vector for the two series' then take the average then make a new time series based on this but I couldn't get that to work.

I 'm not sure but I feel this should be really easy to do as in one line of code but I can't figure it out.

Any help? Thanks!

Suppose we have the series A and B defined reproducibly as shown. Then just add them and divide by 2:

A <- ts(c(1, 2, 4, 10), start = 2018, frequency = 12)
B <- ts(c(4, 6, 10, 4), start = 2018, frequency = 12)
(A+B)/2

giving:

     Jan Feb Mar Apr
2018 2.5 4.0 7.0 7.0

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