【问题标题】:time-series formating: Cannot replicate a time-series format时间序列格式:无法复制时间序列格式
【发布时间】:2018-11-10 03:17:48
【问题描述】:

我正在尝试将我的数据转换为特定的时间序列格式。我尝试制作数据的格式类似于以下时间序列格式;

library(fpp)
data(ausbeer)

> str(ausbeer)
 Time-Series [1:211] from 1956 to 2008: 284 213 227 308 262 228 236 320 272 233 ...

但是我的数据目前如下;

> str(wide_DF)
 Time-Series [1:5, 1:53] from 1 to 5: 2008 2009 2010 2011 2012 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:53] "year" "1" "2" "3" ...

无论我尝试什么,我似乎都无法将其转换为与 ausbeer 数据相同的格式。

我们将不胜感激。

数据:

wide_DF <- structure(c(2008, 2009, 2010, 2011, 2012, 0.149697356812732, 
0.506609550726262, 0.483701772054131, 0.340553948856928, 0.333626779091713, 
0.0244998111324886, 0.112120844942705, 0.0900944558373256, 0, 
0.0415224265012151, 0.0550537737139359, 0.17295508953821, 0.163929433720004, 
0.0576641678554561, 0.0906564289945531, 0.0274508134712055, 0.16192922040378, 
0.142555512496348, 0.0625454708565096, 0.0713307780915137, 0.213984247872558, 
0.388163795230051, 0.164139722545731, 0.0770529539010844, 0.0938540183343052, 
0.0783422018092716, 0.227206496783438, 0.35237013136599, 0.258504104665886, 
0.321155525044278, 0.0310253280164216, 0.145802804370329, 0.235904612124217, 
0.099505662356445, 0.184479613065924, 0.0541816334844162, 0.171606159981382, 
0.115107342701831, 0.0741165588765761, 0.108561719279517, 0.0806492605345567, 
0.154086848847159, 0.128553880389704, 0.0619227096142703, 0.102441840801919, 
0.0877855302949306, 0.210374588670803, 0.168914894757668, 0.0892981276862553, 
0.107796585571731, 0.152766825529036, 0.197064573460434, 0.157147609673816, 
0.0794331221751312, 0.130178451495829, NA, 0.288013610146669, 
0.218033903861127, 0.144165085504355, 0.265694549788369, 0.168423392180753, 
0.220217969236187, 0.192778260148724, 0.0616202640553713, 0.208895233807108, 
0.172908899350928, 0.273558409751774, NA, 0.131826476698887, 
0.214943212753592, 0.185482743591095, NA, 0.264010141661686, 
0.137209722798776, NA, 0.213353668598008, 0.288506341574192, 
0.265934476984103, 0.166437178815794, 0.213012834405297, 0.229097493059307, 
0.326273737259306, 0.209431740094857, NA, 0.240648159088921, 
0.261158363124192, 0.317036580243605, 0.244681209115455, 0.166687664239444, 
0.240465787771525, 0.282936314890266, 0.376241375996475, 0.288711990429523, 
0.218930682309907, 0.294307615813644, 0.340039521860067, 0.381665974567176, 
0.289509990005749, 0.222712288785976, 0.302326040229749, 0.592897079173477, 
0.707453475415865, 0.315092875222347, 0.238746934161925, 0.360467454111782, 
0.437728811188524, 0.485169961326965, 0.686681695697921, 0.513927986995597, 
0.657805801598166, 0.413066850628898, 0.420451063363391, 0.452317417206126, 
0.392680063685442, 0.467494633248041, 0.490885152462683, 0.449702773878119, 
0.374537214449401, 0.314372316775567, 0.352543088557757, 0.456852949424961, 
0.502821656395841, 0.473850571102317, 0.37271347773425, 0.468025614416299, 
0.492964518353547, 0.491841956261615, 0.451832204837682, 0.330054166675406, 
0.452103599554613, 0.972882256833953, 1, 0.836981605987354, 0.735454399633936, 
0.625060089794185, 0.420276672512582, 0.44763479957363, 0.51920428542675, 
0.484249008420553, 0.828415542650317, 0.439876590158875, 0.458798662510525, 
0.446969106246101, 0.329267937698866, 0.402265340895058, 0.443357095278529, 
0.48161107578401, 0.421502554574427, 0.35492302612805, 0.389391661815002, 
0.480802216652516, 0.496614239968388, 0.41709701215027, 0.355395255525041, 
0.427983230181801, 0.426624787626307, 0.47619764751241, 0.390323036410375, 
0.346946500338582, 0.444962482661289, 0.398178487457366, 0.460418831412368, 
0.365705653465875, 0.314414354295281, 0.404995279601097, 0.395484743345358, 
0.447895106385658, 0.333904920716383, 0.315905256117267, 0.38580728350725, 
0.61293865090702, 0.392285202440178, 0.300121453991199, 0.318457847197856, 
0.382196506098525, 0.42777529076777, 0.655937896884758, 0.579486246422688, 
0.512463359506227, 0.601431192394729, 0.283409977946298, 0.430264772601089, 
0.321055545570556, 0.311027552565597, 0.419878449584049, 0.295947790026711, 
0.323869738229137, 0.215519275318642, 0.192393768801782, 0.326484958316528, 
0.317550712975473, 0.303764772399812, 0.215565915142833, 0.177813119709567, 
0.288920671391334, 0.299640010568774, 0.258602815268962, 0.208650826721134, 
0.192887375961921, 0.273866371013686, 0.300719638221296, 0.260930408982457, 
0.214130384575884, 0.20094859121612, 0.262324215127644, 0.291610161608615, 
0.240764266638331, 0.232400949526744, 0.190638711181672, 0.194923630854379, 
0.247095733415861, 0.250696875411684, 0.17072512824086, 0.142654512656176, 
0.221234530015598, 0.336036187889497, 0.337172813493932, 0.241964382857466, 
0.188030459289294, 0.247565234387846, 0.228521023231508, 0.227452403443811, 
0.156667771761189, 0.131392002677444, 0.229392396017928, 0.213955172137217, 
0.229145352317625, 0.145988572682793, 0.1354966579701, 0.164468590746803, 
0.273338090020996, 0.28521986301974, 0.199009246024986, 0.178427989941778, 
0.218632123403024, 0.485758317106326, 0.478231444703654, 0.371723057102618, 
0.358665186970456, 0.437144925882923, 0.143782632825279, 0.132760650342865, 
0.0511910889931185, 0.0421970278185858, 0.0830346125807046, 0.169335703112876, 
0.127452787871597, 0.0947298145120868, 0.0660661513870076, 0.136073219608577, 
0.328666970899003, 0.296331593970631, 0.154669507656273, 0.12819972894051, 
0.134629124753297, 0.491065758190125, 0.47654036029283, 0.357442986752192, 
NA, 0.388556693139287), .Dim = c(5L, 53L), .Dimnames = list(NULL, 
    c("year", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", 
    "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", 
    "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", 
    "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", 
    "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", 
    "51", "52")), .Tsp = c(1, 5, 1), class = c("mts", "ts", "matrix"
))

编辑:

audbeer 形状:

> head(ausbeer, 32)
     Qtr1 Qtr2 Qtr3 Qtr4
1956  284  213  227  308
1957  262  228  236  320
1958  272  233  237  313
1959  261  227  250  314
1960  286  227  260  311
1961  295  233  257  339
1962  279  250  270  346
1963  294  255  278  363

【问题讨论】:

    标签: r time-series


    【解决方案1】:

    我想我明白你现在想要做什么了。让我们转置,并修复列名。

    wide_df2 <- as.data.frame(t(as.data.frame(wide_DF))) 
    # we need as.data.frame() twice here to strip the ts class, and then add the data.frame class back after t()
    

    将第一行设置为列名:

    names(wide_df2) <- wide_df2[1,]
    

    从数据中删除列名:

    wide_df2 <- wide_df2[-1, ]
    

    并转换回ts:

    wide_df2 <- ts(wide_df2)
    
    
             2008      2009       2010       2011       2012
    1  0.14969736 0.5066096 0.48370177 0.34055395 0.33362678
    2  0.02449981 0.1121208 0.09009446 0.00000000 0.04152243
    3  0.05505377 0.1729551 0.16392943 0.05766417 0.09065643
    4  0.02745081 0.1619292 0.14255551 0.06254547 0.07133078
    5  0.21398425 0.3881638 0.16413972 0.07705295 0.09385402
    6  0.07834220 0.2272065 0.35237013 0.25850410 0.32115553
    7  0.03102533 0.1458028 0.23590461 0.09950566 0.18447961
    8  0.05418163 0.1716062 0.11510734 0.07411656 0.10856172
    

    【讨论】:

    • 谢谢!这就是我最初获得数据的方式,但这与ausbeer 数据的形状不同。所以我用下面的把它做成了一样的形状wide_DF &lt;- df %&gt;% spread(week, dollarmm)
    • 谢谢!我希望将年份保留在左侧,将几周保留在顶部。当我运行plot(as.ts(wide_df2)) 时,我得到 5 个地块(每年一个)。我希望得到与ausbeer 相同的格式,因为我正在密切关注本教程。
    • @user113156 可能是ts(wide_DF[ , -1], start = 2008)?
    • 我明白了:Error in plotts(x = x, y = y, plot.type = plot.type, xy.labels = xy.labels, : cannot plot more than 10 series as "multiple"
    • 至少在默认设置下,使用plot 一次不能绘制超过 10 个时间序列。 plot( ts(wide_DF[,-1], start = 2008)[,1:5]) 会工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 2017-12-31
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    相关资源
    最近更新 更多