Reputation: 823
I have a list object with multiple slots, 2 in this example, each containing a time series of monthly data over 5 yrs (60 values). I want to calculate an "average" time series from these two i.e the January value of my new series should be the mean of the 2 Januarys from each of the two time series and so on. I thought of using lapply()
but if I understand correctly, that is used to apply functions within slots and not across, but I could be wrong.
Here is a dput()
of my data:
list(structure(c(-2.70881589936525, -1.25455287657218, 2.20891093254408,
5.47494447650721, 9.22974987813887, 12.0978184689361, 15.8529078203063,
14.5682520133134, 10.8615272853853, 5.13086415717895, 0.728917940858284,
2.13993708024285, 0.0592607633855364, -1.08188244487586, -1.19467731719249,
5.03740002827978, 10.3763483415682, 13.3292119845773, 12.838352493412,
15.3580851547661, 9.4829099091539, 6.56223017400025, 1.36042454896383,
0.899805834524198, -2.13189083053455, -0.083918862391372, 0.994166453698637,
2.71436535566226, 11.3453352141603, 15.0712013841955, 13.7110193822507,
9.8693411661721, 9.60321957581941, 5.2375499185438, -0.184162586424226,
-1.50175258729513, -6.9445058128996, -3.21184575341925, 0.383804323362742,
5.59544079002557, 7.80248270514967, 12.4958346580684, 14.3387761065989,
12.1472112399243, 12.3920738957853, 7.03456285321734, 1.04268672395181,
-1.38758815045495, -3.32477056135693, -0.447356879470411, 4.56295165574707,
5.68189626665318, 6.74697976141299, 12.0703824641417, 16.8904454284777,
14.2920547883889, 12.1655598473256, 6.77734909883441, 3.00180135903895,
1.94856648801937), .Tsp = c(2001, 2005.91666666667, 12), class = "ts"),
structure(c(-1.63889806183691, -3.44715647302858, 0.394739200062096,
5.23920359311239, 9.57664849661865, 14.0415975194851, 16.7884967982053,
13.6157495618028, 10.5269221330342, 7.71132825720641, -0.0288215700483627,
-3.13091409964762, -0.970448606448803, -1.87539827694689,
0.765137214031195, 4.44395722618218, 10.680721392289, 10.3468681880514,
14.3053058161559, 16.3132350056912, 12.8839577165089, 9.98091681764607,
2.69020486688223, 0.290392068555248, -0.924761078500446,
-5.67205756065117, 1.41326224137296, 6.36338872204811, 8.92258840663339,
13.0624643120579, 12.8689225828453, 14.3836922928304, 12.3805992934003,
7.60234172866889, 2.86744304241512, 1.35829952041713, -2.82629733903844,
-0.768552317763034, -0.568688011194226, 3.57676644057355,
4.99664005346314, 11.0140757656585, 15.498475017639, 13.4278279144656,
11.8598222456967, 7.31027938974563, 3.10247804880477, -2.67278197280972,
-2.49516231672057, -3.63941768231319, 1.89945951183736, 4.26424942213747,
9.37058647983393, 14.5133688239731, 14.6719630140624, 15.5022840542944,
13.3686764903323, 6.20332371420166, 3.05229549361941, -0.975912393030021
), .Tsp = c(2001, 2005.91666666667, 12), class = "ts"))
If there is an automated way of doing this it will be great because I will eventually have a list with 1000 ts()
objects each with 600 data points
Thanks.
Upvotes: 0
Views: 296
Reputation: 269441
If L is the list of "ts"
objects then assuming the time index of each component series is the same:
1) rowMeans/cbind
combined <- ts(rowMeans(do.call("cbind", L)))
tsp(combined) <- tsp(L[[1]]) # fix up times
2) Reduce
Reduce("+", L) / length(L)
These should both work even if there are more than 2 components in L
.
Upvotes: 3