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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현