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 8037709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:00:14+00:00 2026-06-05T03:00:14+00:00

Most packages and posts I found apply mean to a fixed size window or

  • 0

Most packages and posts I found apply mean to a fixed size window or the aggregate month/week data. Is it possible to calculate rolling k month average?

For example, for 1 month rolling window, assuming the data is:

Date          Value
2012-05-28    101
2012-05-25     99
2012-05-24    102
....
2012-04-30     78
2012-04-27     82
2012-04-26     77
2012-04-25     75
2012-04-24     76

The first three rolling 1 month windows should be:

1. 2012-05-28 to 2012-04-30
2. 2012-05-25 to 2012-04-26
3. 2012-05-24 to 2012-04-25

Please note that this is NOT the fixed width rolling window. The window actually changes on the daily basis.

  • 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-06-05T03:00:17+00:00Added an answer on June 5, 2026 at 3:00 am

    I used this code to calculate monthly averages based on daily price data.

    #function for extracting month is in the lubridate package
    install.packages(c("plyr", "lubridate"))
    require(plyr); require(lubridate)
    
    #read the daily data
    daily = read.csv("daily_lumber_prices.csv")
    price = daily$Open
    date = daily$Date
    
    #convert date to a usable format
    date = strptime(date, "%d-%b-%y")
    mon = month(date)
    T = length(price)
    
    #need to know when months change
    change_month = rep(0,T)
    
    for(t in 2:T){
      if(mon[t] != mon[t-1]){
        change_month[t-1] = 1
      }
    }
    
    month_avg = rep(0,T)
    total = 0
    days = 0
    
    for(t in 1:T){
      if(change_month[t] == 0){
        #cumulative sums for each variable
        total = total + price[t] 
        days = days + 1
      }
    
      else{
        #need to include the current month in the calculation
        month_avg[t] = (total + price[t]) / (days + 1)
        #reset the variables
        total = 0
        days = 0
      }
    }
    

    So, the variable month_avg is storing the monthly averages.

    Is it something like this? This code accounts for the variable lengths of months. There’s certainly a more efficient way to do it, but this works!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Most of the ASP.NET MVC paging solutions I have found by googling look like
So most Java resources when speaking of packages mention a com.yourcompany.project setup. However, I
Instead of just using urllib does anyone know of the most efficient package for
I am studying how the android.speech package works and I noticed that most of
Most of us write conditionals like: if (resultIndex == 0) ...but occaisionally I come
Most databases support some form of insert into select... statement. insert into a select
Most of my clients require donation and shopping cart integration into their websites. I
Most texts on the C++ standard library mention wstring as being the equivalent of
Most web applications have a Location field, in which uses may enter a Location
Most of our projects are short term and of a promotional nature. As a

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.