Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6634383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:55:23+00:00 2026-05-25T22:55:23+00:00

I have an irregular time series ( xts in R ) that I want

  • 0

I have an irregular time series (xts in R) that I want to apply some time-windowing to. For example, given a time series like the following, I want to compute things like how many observations there are in each discrete 3-hour window, starting from 2009-09-22 00:00:00:

library(lubridate)
s <- xts(c("OK", "Fail", "Service", "OK", "Service", "OK"),
         ymd_hms(c("2009-09-22 07:43:30", "2009-10-01 03:50:30",
                   "2009-10-01 08:45:00", "2009-10-01 09:48:15",
                   "2009-11-11 10:30:30", "2009-11-11 11:12:45")))

I apparently can’t use period.apply() or split() to do it, because those will omit periods with no observations, and I can’t give it a starting time.

My desired output for the simple counting problem (though, of course, my real tasks are more complicated with each segment!) would be something like this if I aggregated 3 days at a time:

2009-09-22    1
2009-09-25    0
2009-09-28    0
2009-10-01    3
2009-10-04    0
2009-10-07    0
2009-10-10    0
2009-10-13    0
2009-10-16    0
2009-10-19    0
2009-10-22    0
2009-10-25    0
2009-10-28    0
2009-10-31    0
2009-11-03    0
2009-11-06    0
2009-11-09    2

Thanks for any guidance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:55:24+00:00Added an answer on May 25, 2026 at 10:55 pm

    Use align.time to put the index of s into the periods you’re interested in. Then use period.apply to find the length of each 3-hour window. Then merge it with an empty xts object that has all the index values you want.

    # align index into 3-hour blocks
    a <- align.time(s, n=60*60*3)
    # find the number of obs in each block
    count <- period.apply(a, endpoints(a, "hours", 3), length)
    # create an empty xts object with the desired index
    e <- xts(,seq(start(a),end(a),by="3 hours"))
    # merge the counts with the empty object and fill with zeros
    out <- merge(e,count,fill=0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have some multivariate irregular time series, such as zoo or xts objects
I have an irregular time series that I'm working with that I'd like to
I have an irregular time series contained in xts and separate time index vector
I have an irregular(?) time series data points in a day as following Jun
I have an irregular time series and am using xts 's endpoints to get
I have a time series of temperature profiles that I want to interpolate, I
I have a ListView that gets updated frequently, at irregular intervals. I want it
Is it possible to have irregular shapes like anchors on some surface in JS
I need to add two irregular time series (covering business days). I have two
I have a whole bunch of data.frames with irregular time spacing. I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.