Fill gaps between dates in xtsInsert rows for missing dates/timesTime series (xts) strptime; ONLY month and dayFill in time series gaps with both LCOF and NOCB methods but acknowledge breaks in time seriesHow to use the dates and times functions for preparing a sequence of 4 times per day, so every 6 hoursCreating a time series in R with a row every 15 minutesHow should I deal with 'from' must be of length 1 error?Does R's Time-Series automatically generate missing data?R: Filling timeseries values but only within last 12 monthsGrouped gap filling in Postgresql / Timescaledbfill NA in timeseries from same date and time, different years rExtract day and month from date

As a GM, is it bad form to ask for a moment to think when improvising?

Can you use "едать" and "игрывать" in the present and future tenses?

Should homeowners insurance cover the cost of the home?

Is 'contemporary' ambiguous and if so is there a better word?

Are the Night's Watch still required?

How do I, as a DM, handle a party that decides to set up an ambush in a dungeon?

Any examples of liquids volatile at room temp but non-flammable?

Is “snitty” a popular American English term? What is its origin?

Which US defense organization would respond to an invasion like this?

To kill a cuckoo

Can there be a single technologically advanced nation, in a continent full of non-technologically advanced nations?

Formatting Datetime.now()

Dangerous workplace travelling

chromatic descent on minor chord

Would you use "llamarse" for an animal's name?

Prove that a definite integral is an infinite sum

Why is my arithmetic with a long long int behaving this way?

Does "Captain Marvel" contain spoilers for "Avengers: Infinity War"?

What do I do if my advisor made a mistake?

Hostile Divisor Numbers

What are the advantages of luxury car brands like Acura/Lexus over their sibling non-luxury brands Honda/Toyota?

Why would a military not separate its forces into different branches?

Is there an age requirement to play in Adventurers League?

How do I calculate how many of an item I'll have in this inventory system?



Fill gaps between dates in xts


Insert rows for missing dates/timesTime series (xts) strptime; ONLY month and dayFill in time series gaps with both LCOF and NOCB methods but acknowledge breaks in time seriesHow to use the dates and times functions for preparing a sequence of 4 times per day, so every 6 hoursCreating a time series in R with a row every 15 minutesHow should I deal with 'from' must be of length 1 error?Does R's Time-Series automatically generate missing data?R: Filling timeseries values but only within last 12 monthsGrouped gap filling in Postgresql / Timescaledbfill NA in timeseries from same date and time, different years rExtract day and month from date






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















So I have a very basic question. Let's say we have a few date gaps in a time series object, and I want to fill those gaps with an arbitrary value. For example let's say we have:



i <- c(seq.Date(from = as.Date("2015-01-01", format = "%Y-%m-%d"), to = as.Date("2016-01-01", format = "%Y-%m-%d"), by = "month"),
seq.Date(from = as.Date("2017-01-01", format = "%Y-%m-%d"), to = as.Date("2018-01-01", format = "%Y-%m-%d"),by = "month"))

ts <- xts(rep(0,length(i)), order.by = i)

[,1]
2015-01-01 0
2015-02-01 0
2015-03-01 0
2015-04-01 0
2015-05-01 0
2015-06-01 0
2015-07-01 0
2015-08-01 0
2015-09-01 0
2015-10-01 0
2015-11-01 0
2015-12-01 0
2016-01-01 0
2017-01-01 0
2017-02-01 0
2017-03-01 0
2017-04-01 0
2017-05-01 0
2017-06-01 0
2017-07-01 0
2017-08-01 0
2017-09-01 0
2017-10-01 0
2017-11-01 0
2017-12-01 0
2018-01-01 0


What I wish to achieve is "fill" the time series ts for all months between two arbitrary dates i.e. start.date and end.date with a 1. Any suggestions?



My attempt:



 if(index(ts)[1] > start.date)
len.aux <- length(seq(from = start.date, to = index(ts)[1] %m-% months(1), by = "month"))
ts <- c(xts(rep(1, len.aux), order.by = seq.Date(from = start.date, to = index(ts)[1] %m-% months(1), by = "month")), ts)

if(index(ts)[length(ts)] < end.date)
len.aux <- length(seq(from = index(ts)[length(ts)] %m+% months(1), to = end.date, by = "month"))
ts <- c(ts, xts(rep(1, len.aux), order.by = seq.Date(from = index(ts)[length(ts)] %m+% months(1), to = end.date, by = "month")))



However, this only fills the gaps for the "tails" of the series and doesn't fill gaps in between.



Thanks for the help!



Please note that this is only a minimal working example of my issue










share|improve this question






















  • you mean it should fill all the dates from feb 2016 to dec 2016 and fill 1 as the value?

    – noob
    Mar 23 at 3:13











  • All the dates between an arbitrary start and end date which are not accounted for in the original series. So we could say from Jan 15 until October 18 (by month)

    – migueldva
    Mar 23 at 3:16











  • Possible duplicate of Insert rows for missing dates/times

    – phiver
    Mar 23 at 8:53

















1















So I have a very basic question. Let's say we have a few date gaps in a time series object, and I want to fill those gaps with an arbitrary value. For example let's say we have:



i <- c(seq.Date(from = as.Date("2015-01-01", format = "%Y-%m-%d"), to = as.Date("2016-01-01", format = "%Y-%m-%d"), by = "month"),
seq.Date(from = as.Date("2017-01-01", format = "%Y-%m-%d"), to = as.Date("2018-01-01", format = "%Y-%m-%d"),by = "month"))

ts <- xts(rep(0,length(i)), order.by = i)

[,1]
2015-01-01 0
2015-02-01 0
2015-03-01 0
2015-04-01 0
2015-05-01 0
2015-06-01 0
2015-07-01 0
2015-08-01 0
2015-09-01 0
2015-10-01 0
2015-11-01 0
2015-12-01 0
2016-01-01 0
2017-01-01 0
2017-02-01 0
2017-03-01 0
2017-04-01 0
2017-05-01 0
2017-06-01 0
2017-07-01 0
2017-08-01 0
2017-09-01 0
2017-10-01 0
2017-11-01 0
2017-12-01 0
2018-01-01 0


What I wish to achieve is "fill" the time series ts for all months between two arbitrary dates i.e. start.date and end.date with a 1. Any suggestions?



My attempt:



 if(index(ts)[1] > start.date)
len.aux <- length(seq(from = start.date, to = index(ts)[1] %m-% months(1), by = "month"))
ts <- c(xts(rep(1, len.aux), order.by = seq.Date(from = start.date, to = index(ts)[1] %m-% months(1), by = "month")), ts)

if(index(ts)[length(ts)] < end.date)
len.aux <- length(seq(from = index(ts)[length(ts)] %m+% months(1), to = end.date, by = "month"))
ts <- c(ts, xts(rep(1, len.aux), order.by = seq.Date(from = index(ts)[length(ts)] %m+% months(1), to = end.date, by = "month")))



However, this only fills the gaps for the "tails" of the series and doesn't fill gaps in between.



Thanks for the help!



Please note that this is only a minimal working example of my issue










share|improve this question






















  • you mean it should fill all the dates from feb 2016 to dec 2016 and fill 1 as the value?

    – noob
    Mar 23 at 3:13











  • All the dates between an arbitrary start and end date which are not accounted for in the original series. So we could say from Jan 15 until October 18 (by month)

    – migueldva
    Mar 23 at 3:16











  • Possible duplicate of Insert rows for missing dates/times

    – phiver
    Mar 23 at 8:53













1












1








1








So I have a very basic question. Let's say we have a few date gaps in a time series object, and I want to fill those gaps with an arbitrary value. For example let's say we have:



i <- c(seq.Date(from = as.Date("2015-01-01", format = "%Y-%m-%d"), to = as.Date("2016-01-01", format = "%Y-%m-%d"), by = "month"),
seq.Date(from = as.Date("2017-01-01", format = "%Y-%m-%d"), to = as.Date("2018-01-01", format = "%Y-%m-%d"),by = "month"))

ts <- xts(rep(0,length(i)), order.by = i)

[,1]
2015-01-01 0
2015-02-01 0
2015-03-01 0
2015-04-01 0
2015-05-01 0
2015-06-01 0
2015-07-01 0
2015-08-01 0
2015-09-01 0
2015-10-01 0
2015-11-01 0
2015-12-01 0
2016-01-01 0
2017-01-01 0
2017-02-01 0
2017-03-01 0
2017-04-01 0
2017-05-01 0
2017-06-01 0
2017-07-01 0
2017-08-01 0
2017-09-01 0
2017-10-01 0
2017-11-01 0
2017-12-01 0
2018-01-01 0


What I wish to achieve is "fill" the time series ts for all months between two arbitrary dates i.e. start.date and end.date with a 1. Any suggestions?



My attempt:



 if(index(ts)[1] > start.date)
len.aux <- length(seq(from = start.date, to = index(ts)[1] %m-% months(1), by = "month"))
ts <- c(xts(rep(1, len.aux), order.by = seq.Date(from = start.date, to = index(ts)[1] %m-% months(1), by = "month")), ts)

if(index(ts)[length(ts)] < end.date)
len.aux <- length(seq(from = index(ts)[length(ts)] %m+% months(1), to = end.date, by = "month"))
ts <- c(ts, xts(rep(1, len.aux), order.by = seq.Date(from = index(ts)[length(ts)] %m+% months(1), to = end.date, by = "month")))



However, this only fills the gaps for the "tails" of the series and doesn't fill gaps in between.



Thanks for the help!



Please note that this is only a minimal working example of my issue










share|improve this question














So I have a very basic question. Let's say we have a few date gaps in a time series object, and I want to fill those gaps with an arbitrary value. For example let's say we have:



i <- c(seq.Date(from = as.Date("2015-01-01", format = "%Y-%m-%d"), to = as.Date("2016-01-01", format = "%Y-%m-%d"), by = "month"),
seq.Date(from = as.Date("2017-01-01", format = "%Y-%m-%d"), to = as.Date("2018-01-01", format = "%Y-%m-%d"),by = "month"))

ts <- xts(rep(0,length(i)), order.by = i)

[,1]
2015-01-01 0
2015-02-01 0
2015-03-01 0
2015-04-01 0
2015-05-01 0
2015-06-01 0
2015-07-01 0
2015-08-01 0
2015-09-01 0
2015-10-01 0
2015-11-01 0
2015-12-01 0
2016-01-01 0
2017-01-01 0
2017-02-01 0
2017-03-01 0
2017-04-01 0
2017-05-01 0
2017-06-01 0
2017-07-01 0
2017-08-01 0
2017-09-01 0
2017-10-01 0
2017-11-01 0
2017-12-01 0
2018-01-01 0


What I wish to achieve is "fill" the time series ts for all months between two arbitrary dates i.e. start.date and end.date with a 1. Any suggestions?



My attempt:



 if(index(ts)[1] > start.date)
len.aux <- length(seq(from = start.date, to = index(ts)[1] %m-% months(1), by = "month"))
ts <- c(xts(rep(1, len.aux), order.by = seq.Date(from = start.date, to = index(ts)[1] %m-% months(1), by = "month")), ts)

if(index(ts)[length(ts)] < end.date)
len.aux <- length(seq(from = index(ts)[length(ts)] %m+% months(1), to = end.date, by = "month"))
ts <- c(ts, xts(rep(1, len.aux), order.by = seq.Date(from = index(ts)[length(ts)] %m+% months(1), to = end.date, by = "month")))



However, this only fills the gaps for the "tails" of the series and doesn't fill gaps in between.



Thanks for the help!



Please note that this is only a minimal working example of my issue







r time-series xts






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 23 at 1:05









migueldvamigueldva

333




333












  • you mean it should fill all the dates from feb 2016 to dec 2016 and fill 1 as the value?

    – noob
    Mar 23 at 3:13











  • All the dates between an arbitrary start and end date which are not accounted for in the original series. So we could say from Jan 15 until October 18 (by month)

    – migueldva
    Mar 23 at 3:16











  • Possible duplicate of Insert rows for missing dates/times

    – phiver
    Mar 23 at 8:53

















  • you mean it should fill all the dates from feb 2016 to dec 2016 and fill 1 as the value?

    – noob
    Mar 23 at 3:13











  • All the dates between an arbitrary start and end date which are not accounted for in the original series. So we could say from Jan 15 until October 18 (by month)

    – migueldva
    Mar 23 at 3:16











  • Possible duplicate of Insert rows for missing dates/times

    – phiver
    Mar 23 at 8:53
















you mean it should fill all the dates from feb 2016 to dec 2016 and fill 1 as the value?

– noob
Mar 23 at 3:13





you mean it should fill all the dates from feb 2016 to dec 2016 and fill 1 as the value?

– noob
Mar 23 at 3:13













All the dates between an arbitrary start and end date which are not accounted for in the original series. So we could say from Jan 15 until October 18 (by month)

– migueldva
Mar 23 at 3:16





All the dates between an arbitrary start and end date which are not accounted for in the original series. So we could say from Jan 15 until October 18 (by month)

– migueldva
Mar 23 at 3:16













Possible duplicate of Insert rows for missing dates/times

– phiver
Mar 23 at 8:53





Possible duplicate of Insert rows for missing dates/times

– phiver
Mar 23 at 8:53












1 Answer
1






active

oldest

votes


















0














You may use the tsibble package:



library(tsibble)
i <- c(seq.Date(from = as.Date("2015-01-01", format = "%Y-%m-%d"), to = as.Date("2016-01-01", format = "%Y-%m-%d"), by = "month"),
seq.Date(from = as.Date("2017-01-01", format = "%Y-%m-%d"), to = as.Date("2018-01-01", format = "%Y-%m-%d"),by = "month"))

tsibble(datetime = yearmonth(i),
value = 0, index = datetime) %>%
fill_gaps(value = 1) %>%
View()


The yearmonth function will ensure that the index is monthly (daily is default). The function fill_gaps will then include missing months and will set missing values in column value to 1 (default is NA).






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55309630%2ffill-gaps-between-dates-in-xts%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You may use the tsibble package:



    library(tsibble)
    i <- c(seq.Date(from = as.Date("2015-01-01", format = "%Y-%m-%d"), to = as.Date("2016-01-01", format = "%Y-%m-%d"), by = "month"),
    seq.Date(from = as.Date("2017-01-01", format = "%Y-%m-%d"), to = as.Date("2018-01-01", format = "%Y-%m-%d"),by = "month"))

    tsibble(datetime = yearmonth(i),
    value = 0, index = datetime) %>%
    fill_gaps(value = 1) %>%
    View()


    The yearmonth function will ensure that the index is monthly (daily is default). The function fill_gaps will then include missing months and will set missing values in column value to 1 (default is NA).






    share|improve this answer



























      0














      You may use the tsibble package:



      library(tsibble)
      i <- c(seq.Date(from = as.Date("2015-01-01", format = "%Y-%m-%d"), to = as.Date("2016-01-01", format = "%Y-%m-%d"), by = "month"),
      seq.Date(from = as.Date("2017-01-01", format = "%Y-%m-%d"), to = as.Date("2018-01-01", format = "%Y-%m-%d"),by = "month"))

      tsibble(datetime = yearmonth(i),
      value = 0, index = datetime) %>%
      fill_gaps(value = 1) %>%
      View()


      The yearmonth function will ensure that the index is monthly (daily is default). The function fill_gaps will then include missing months and will set missing values in column value to 1 (default is NA).






      share|improve this answer

























        0












        0








        0







        You may use the tsibble package:



        library(tsibble)
        i <- c(seq.Date(from = as.Date("2015-01-01", format = "%Y-%m-%d"), to = as.Date("2016-01-01", format = "%Y-%m-%d"), by = "month"),
        seq.Date(from = as.Date("2017-01-01", format = "%Y-%m-%d"), to = as.Date("2018-01-01", format = "%Y-%m-%d"),by = "month"))

        tsibble(datetime = yearmonth(i),
        value = 0, index = datetime) %>%
        fill_gaps(value = 1) %>%
        View()


        The yearmonth function will ensure that the index is monthly (daily is default). The function fill_gaps will then include missing months and will set missing values in column value to 1 (default is NA).






        share|improve this answer













        You may use the tsibble package:



        library(tsibble)
        i <- c(seq.Date(from = as.Date("2015-01-01", format = "%Y-%m-%d"), to = as.Date("2016-01-01", format = "%Y-%m-%d"), by = "month"),
        seq.Date(from = as.Date("2017-01-01", format = "%Y-%m-%d"), to = as.Date("2018-01-01", format = "%Y-%m-%d"),by = "month"))

        tsibble(datetime = yearmonth(i),
        value = 0, index = datetime) %>%
        fill_gaps(value = 1) %>%
        View()


        The yearmonth function will ensure that the index is monthly (daily is default). The function fill_gaps will then include missing months and will set missing values in column value to 1 (default is NA).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 26 at 9:14









        Simon MüllerSimon Müller

        31825




        31825





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid …


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55309630%2ffill-gaps-between-dates-in-xts%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

            Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?