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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:02:18+00:00 2026-06-01T13:02:18+00:00

I am trying to calculate a rolling mean using plyr. The data is at

  • 0

I am trying to calculate a rolling mean using plyr. The data is at the industry-country-year, with repeated observations for each industry-country. The data is unbalanced, but most industry-countries have approximately 15 observations.

For example the data looks like this:

country       ISIC      Year      Value
Algeria        1        1990       400
Algeria        1        1991       450
Algeria        1        1992       460
Algeria        2        1990       450
Algeria        2        1991       500
Algeria        2        1992       450
Argentina      1        1990       400
Argentina      1        1991       450
Argentina      1        1992       460
Argentina      2        1990       450
Argentina      2        1991       500
Argentina      2        1992       450
.              .        .          .
.              .        .          .

If I subset the data to a specific industry and country I am able to calculate the rolling mean like this

rollmean(subdata$Value, 3)

However, I’ve been unable to get it to work with plyr, so as to calculate the rolling mean for each industry-country group.
I’ve tried:

roll <- ddply(data, .(country, ISIC), summarize, rollmean(data$Value, 3))
  • 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-01T13:02:19+00:00Added an answer on June 1, 2026 at 1:02 pm

    a rolling mean necessarily shortens the data which part of why you get the error.

    ddply(dat, .(country, ISIC), function(df) data.frame(country=unique(df$country),                  
                                                         ISIC=unique(df$ISIC),
                                                         rolled=rollmean(df$Value, 3)))
        country ISIC   rolled
    1   Algeria    1 436.6667
    2   Algeria    2 466.6667
    3 Argentina    1 436.6667
    4 Argentina    2 466.6667
    

    However, if you’re doing a rolling mean on 3 samples and your data only has 3 samples, you’re just calculating the mean:

    ddply(dat, .(country, ISIC), summarise, mean(Value))
    
        country ISIC      ..1
    1   Algeria    1 436.6667
    2   Algeria    2 466.6667
    3 Argentina    1 436.6667
    4 Argentina    2 466.6667
    

    UPDATED FOR COMMENTS:

    To return the dates you can use the na.pad argument to rollmean:

    ddply(dat, .(country, ISIC), function(df) {df$rolled <- rollmean(df$Value, 3, na.pad=TRUE); return(df)})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

am trying to calculate mean and variance using 3X3 window over image(hXw) in opencv...here
I'm trying to calculate the Mean Difference average of a set of data. I
I am trying to calculate the fiscal year from this following code. But I
I am trying to calculate a determinant using the boost c++ libraries. I found
I'm trying to calculate the sunset / rise times using python based on the
I am trying to calculate exponential moving average on 15 day bars, but want
I'm trying to calculate the total amount won for each user_name in the For
I'm trying to calculate (dynamically) the % of the year that has elapsed, or
I'm trying to calculate various time period returns (monthly, quarterly, yearly etc.) for each
Im trying to calculate the bitrate from a video, but Im not sure if

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.