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

  • Home
  • SEARCH
  • 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 8702581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:41:16+00:00 2026-06-13T02:41:16+00:00

I get why vectorised functions are better than for-loops. But there are some problems

  • 0

I get why vectorised functions are better than for-loops.

But there are some problems where I can’t see the vectorised functional programming solution. One of those is summing monthly data to get quarterly data. Any suggestions to replace this code …

month <- 1:100
A422072L <- c(rep(NA, 4), rnorm(96, 100, 5) ) + 2 * month
A422070J <- c(NA, NA, rnorm(96, 100, 5), NA, NA) + 2 * month
Au.approvals <- data.frame(month=month, A422072L=A422072L, A422070J=A422070J)

Au.approvals$trend.sum.A422072L.qtr <- NA
Au.approvals$sa.sum.A422070J.qtr <- NA
for(i in seq_len(nrow(Au.approvals)))
{
    if(i < 3) next
    if(all(!is.na(Au.approvals$A422072L[(i-2):i])))
        Au.approvals$trend.sum.A422072L.qtr[i] <- sum(Au.approvals$A422072L[(i-2):i])
    if(all(!is.na(Au.approvals$A422070J[(i-2):i])))
        Au.approvals$sa.sum.A422070J.qtr[i]    <- sum(Au.approvals$A422070J[(i-2):i])
}

print(Au.approvals)

Now with enough data to run as an example.

  • 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-13T02:41:17+00:00Added an answer on June 13, 2026 at 2:41 am

    Let’s create some bogus timeseries:

    time_dat = data.frame(t = 1:100, value = runif(100))
    

    To get a rolling sum, please take a look at rollapply from the zoo package:

    require(zoo)
    time_dat = transform(time_dat, 
                         roll_value = rollapply(value, 10, sum, fill = TRUE))
    

    here I assume that the coarser resolution (quarterly) is 10 times coarser than the finer resolution.


    Original answer for a non-rolling mean:

    I like to use the functions from the plyr package, but ave, aggregate, and data.table are also good options. For large datasets, data.table is veeery fast. But to get back to some plyr magic:

    First create an additional column which specifies the more coarse time frequency, i.e. which quarter is your observation in:

    time_dat[["coarse_t"]] = rep(1:10, each = 10)
    > head(time_dat)
      t     value coarse_t
    1 1 0.9045097        1
    2 2 0.4174182        1
    3 3 0.5638139        1
    4 4 0.8228698        1
    5 5 0.7059027        1
    6 6 0.5285386        1
    

    Now we can aggregate time_dat for the coarser time frequency:

    time_dat_coarse = ddply(time_dat, .(coarse_t), summarise, sum_value = sum(value))
    > time_dat_coarse
       coarse_t sum_value
    1         1  6.097348
    2         2  4.834720
    3         3  3.988809
    4         4  4.170656
    5         5  4.538269
    6         6  6.198716
    7         7  4.399282
    8         8  5.507384
    9         9  6.089072
    10       10  4.663287
    

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

Sidebar

Related Questions

I want to get better at vectorizing my loops in MATLAB. At the moment,
I am new to R and am trying to find a better solution for
I know Java is a secure language but when matrix calculations are needed, can
In MATLAB, vectorized code is faster than using for-loops. I have been trying to
Get an empty HTML, type in this and see its source code on Google
GET_COOKIE('CURRENT_IM') will return a number saved in the CURRENT_IM cookie. How can I get
Get class from div inside an li and add to the same li. The
//Get width and resize another element $(document).ready(function() { function ResizeSearch(GridID, SearchID) { var eleWidth
get an array of bluetooth ID's Broadcast the bluetooth signal manually
GET is a convenient method to post the form id, post the website id

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.