我有一个data.frame,其中保存了2007-2018年的股票收益值。我想将data.frame变成一个时间序列对象,该对象具有以年为行和以月为列的时间。但我严格希望使用时间序列对象。这篇文章的底部是数据。非常感谢你。
#data is the data.frame
head(data)
month year value
1 Jan 2007 NA
2 Feb 2007 0.006786261
3 Mar 2007 0.009286089
4 Apr 2007 0.005629739
5 May 2007 0.007331115
#I would like the data.frame to look like this
Jan Feb March . . .
2007 .007 .004 .002
2008 .. ..
2009
.
.
dput(data)
structure(list(month = structure(c(5L, 4L, 8L, 1L, 9L, 7L, 6L,
2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L,
10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L,
4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L,
9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L,
2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L,
10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L,
4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L,
9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L,
2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L,
10L, 3L, 5L, 4L, 8L), .Label = c("Apr", "Aug", "Dec", "Feb",
"Jan", "Jul", "Jun", "Mar", "May", "Nov", "Oct", "Sep"), class = "factor"),
year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L), .Label = c("2007",
"2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015",
"2016", "2017", "2018", "2019"), class = "factor"), value = c(NA,
0.0067862609449896, 0.00928608882417802, 0.00562973878197353,
0.00733111542875031, 0.0087482496649306, 0.00936694578199713,
0.0144756868560624, 0.0105608831578794, 0.0101284150490755,
0.0167576065587261, 0.0138785280404449, 0.0143441502708488,
0.0125829938485431, 0.0179892150013815, 0.00970615658405921,
0.00768883059521547, 0.0111786714764919, 0.0145189269960768,
0.0118312537651467, 0.0247339829341687, 0.046892536673157,
0.0401918613789653, 0.0316855246848506, 0.0251940254090586,
0.0183064351107808, 0.0326696560789222, 0.0218561958791411,
0.0177512645864564, 0.0111690443960406, 0.0123102970162703,
0.012100621781721, 0.0106966310934588, 0.0137096033783715,
0.0119726911263377, 0.00854463124594601, 0.0108015831981558,
0.00931088186542605, 0.00746920472947338, 0.0101701715588112,
0.0213924034454435, 0.0162874759373191, 0.0157981684648648,
0.0133829223638553, 0.0117656491021291, 0.00745196804972731,
0.00980528604931585, 0.00554097522471432, 0.00918236878274663,
0.00887297962781599, 0.0100436263340333, 0.00696697700287339,
0.0108449928319291, 0.0102979364108851, 0.0102462979067078,
0.0294071090799894, 0.0210901267317123, 0.0247406432389061,
0.0204372107421992, 0.0126339550058995, 0.00595109536796898,
0.00843638349223728, 0.00876871853405711, 0.0111057663923942,
0.00821384207850501, 0.0134833755590203, 0.00841619682440934,
0.00619761855452945, 0.00694031400129975, 0.00567646376323664,
0.00886632175296176, 0.00568213041197199, 0.0053257983548932,
0.00761898661127413, 0.00442066097583478, 0.0099647750946152,
0.00780379519259372, 0.0106194033089023, 0.0055663528395258,
0.00782953045883844, 0.00498479728068384, 0.00814334262913491,
0.00631028410055463, 0.00606286880170242, 0.00872505817226371,
0.00809869924455681, 0.00703030768141687, 0.00995545756513343,
0.00858267765340753, 0.00579981222100005, 0.00777719919050648,
0.00536024280306084, 0.00786286239640707, 0.0117654722362987,
0.00539694766605238, 0.00949773196064378, 0.010611639165364,
0.00510617390903635, 0.0068536884809862, 0.00589832669606801,
0.00501481063402127, 0.00607810113979378, 0.00655939342442367,
0.0110051052192091, 0.0107385037811658, 0.0101636482414476,
0.00760721043356472, 0.0106408300934207, 0.0151709344287119,
0.010570951640466, 0.0101524718974907, 0.00749998119503098,
0.00863071329742892, 0.0111081065813596, 0.00611510339900798,
0.00500391487931314, 0.0091400801490866, 0.00587195613285983,
0.00962305515170221, 0.00704789713936655, 0.00785506546883425,
0.005951215227259, 0.00644779336190417, 0.00725029868519772,
0.00510597797687318, 0.00617574758823582, 0.00502832675286373,
0.00598790764504406, 0.00446934444155502, 0.00398882864191216,
0.00468857073573725, 0.00542554076007646, 0.00575547002132221,
0.0114788091919978, 0.00942261813405381, 0.00822033262153194,
0.00523749256771861, 0.00557040443235175, 0.00649532113208686,
0.00445962427027049, 0.00390384886773907, 0.0124751176302628,
0.010774431114885, 0.0152156311075145, 0.0103009797497185,
0.00515023595279187, 0.00706518930218188)), row.names = c(NA,
-147L), class = "data.frame")