Search code examples
rxts

What is the best way to expand the xts object and fill with NA?


Say i have the following xts object. How do I and what is the best way to expand 20 more rows and fill all the entries of the new rows with NA ?

structure(c(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, -0.626453810742332, 
0.183643324222082, -0.835628612410047, 1.59528080213779, 0.329507771815361, 
-0.820468384118015, 0.487429052428485, 0.738324705129217, 0.575781351653492, 
-0.305388387156356, 1.51178116845085, 0.389843236411431, -0.621240580541804, 
-2.2146998871775, 1.12493091814311, -0.0449336090152309, -0.0161902630989461, 
0.943836210685299, 0.821221195098089, 0.593901321217509, 0.918977371608218, 
0.782136300731067, 0.0745649833651906, -1.98935169586337, 0.61982574789471, 
-0.0561287395290008, -0.155795506705329, -1.47075238389927, -0.47815005510862, 
2.83588312039941, 4.71735910305809, 1.79442454531401, 2.77534322311874, 
1.89238991883419, -0.754119113657213, 1.17001087340064, 1.2114200925793, 
1.88137320657763, 4.20005074396777, 3.52635149691509, 1.67095280749283, 
1.49327663972698, 3.39392675080947, 3.11332639734731, 0.62248861090096, 
0.585009686075761, 2.72916392427366, 3.53706584903083, 1.77530757569954, 
3.76221545290843, 2.79621176073414, 0.775947213498458, 2.68223938284885, 
-0.258726192161585, 4.86604740340207, 5.96079979701172, 1.26555704706698, 
-0.0882692526330615, 4.70915888232724, 2.59483618835753, 10.2048532815143, 
2.88227999180049, 5.06921808735233, 3.084006476342, 0.770180373352784, 
3.56637689854303, -2.41487588667311, 7.39666458468866, 3.45976001463569, 
9.51783501108646, 4.42652858669899, 0.870160707234557, 4.83217906046716, 
0.197707105067245, -0.760900200717306, 3.87433870655239, 1.6701243803447, 
3.00331605489487, 3.22302397245499, 1.23143716143578, 1.29399380154449, 
2.5944641546285, 6.53426098971961, -1.57070040128929, 4.78183856288526, 
3.99885111364055, 6.18929951182909), .Dim = c(29L, 4L), .Dimnames = list(
    NULL, c("x", "y1", "y2", "y3")), index = structure(c(1167667200, 
1167753600, 1167840000, 1167926400, 1168012800, 1168099200, 1168185600, 
1168272000, 1168358400, 1168444800, 1168531200, 1168617600, 1168704000, 
1168790400, 1168876800, 1168963200, 1169049600, 1169136000, 1169222400, 
1169308800, 1169395200, 1169481600, 1169568000, 1169654400, 1169740800, 
1169827200, 1169913600, 1.17e+09, 1170086400), tzone = "", tclass = c("POSIXct", 
"POSIXt")), class = c("xts", "zoo"))

Solution

  • Best way is always debatable. But the following works without any other packages. I use seq to create the newly wanted dates, starting from the last timestamp of the xts object. Add 1 day (60*60*24 seconds) to that and end after 20 days.

    Then it is just a question of merging and the NA's are created automatically.

    library(xts)
    
    # create additional sequence of dates. 
    new <- seq(from = end(my_xts) + 60*60*24, 
        to = end(my_xts) + 20*60*60*24, 
        by = "day")
    
    
    my_xts_new <- merge(my_xts, new)
    
    tail(my_xts_new)
                         x y1 y2 y3
    2007-02-13 17:00:00 NA NA NA NA
    2007-02-14 17:00:00 NA NA NA NA
    2007-02-15 17:00:00 NA NA NA NA
    2007-02-16 17:00:00 NA NA NA NA
    2007-02-17 17:00:00 NA NA NA NA
    2007-02-18 17:00:00 NA NA NA NA