I would like to generate a list of time series based on the column indexs of a master time series (m)
head(cols)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18]
[1,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[2,] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[3,] 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5
[4,] 4 5 6 7 8 9 10 11 5 6 7 8 9 10 11 6 7 8
so for example the first data.frame in the list would be made from m[,1], m[,2], m[,3] and m[,4] and look something like
1 2 3 4
2012-01-01 -0.0248361511 0.0127908458 -0.011976191 0.009987137
2012-01-02 -0.0005351887 0.0290350115 0.004208001 0.007078312
2012-01-03 -0.0016072867 0.0042660695 0.008660648 -0.018255748
2012-01-04 0.0016072867 -0.0001303243 0.001782532 -0.004775416
2012-01-05 0.0026730837 -0.0038740336 -0.007149271 0.015511091
And the 14th would be something like
1 2 4 10
2012-01-01 -0.0248361511 0.0127908458 0.009987137 0.0051973431
2012-01-02 -0.0005351887 0.0290350115 0.007078312 0.0081517268
2012-01-03 -0.0016072867 0.0042660695 -0.018255748 -0.0008121889
2012-01-04 0.0016072867 -0.0001303243 -0.004775416 0.0071366761
2012-01-05 0.0026730837 -0.0038740336 0.015511091 0.0186782999
is there some function I can write or use in the form
apply(cols,2, function(x) DOSOMETHINGLIKE"a <- merge(a,m[x])"HERE)
Some sample data:
Verify the
classof the data to see that it is not a standarddata.frame:Make a sample matrix from which we will be selecting the columns from the TS object created above:
Use
lapplyto subset according to the column-wise values from the above matrix:Verify the
classes of the objects in this list:In the future please provide a reproducible example and be specific in your question about what you are actually working with (for example, which packages you have used to create your data, what is currently loaded, and so on). Your question clearly states you are working with
data.frames, but your comment to my original answer reveals you’re actually working with atimeSeriesobject from thetimeSeriespackage which requires a (slightly) different approach.