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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:06:51+00:00 2026-05-16T05:06:51+00:00

I have a set a data that I need to calculate their consecutive mean

  • 0

I have a set a data that I need to calculate their “consecutive mean” (I dunno if it is the correct name, but I can’t find anything better), here is an example:

ID  Var2 Var3    
1    A    1
2    A    3
3    A    5
4    A    7
5    A    9
6    A    11
7    B    2
8    B    4
9    B    6
10   B    8
11   B    10

Here I need to calculated the mean of 3 Var3 variable in the same subset consecutively (i.e. there will be 4 means caulculated for A: mean(1,3,5), mean(3,5,7), mean(5,7,9), mean(7,9,11), and 3 means calculated for B: mean(2,4,6), mean(4,6,8), mean(6,8,10). And the result should be:

ID  Var2 Var3 Mean
1    A    1   N/A
2    A    3   N/A
3    A    5   3
4    A    7   5
5    A    9   7
6    A    11  9
7    B    2   N/A
8    B    4   N/A
9    B    6   4
10   B    8   6
11   B    10  8

Currently I am using a “loop-inside-a-loop” approach, I subset the dataset using Var2, and then I calculated the mean in another start from the third data.

It suits what I need, but it is very slow, is there any faster way for this problem?

Thanks!

  • 1 1 Answer
  • 1 View
  • 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-16T05:06:52+00:00Added an answer on May 16, 2026 at 5:06 am

    It’s generally referred to as a “rolling mean” or “running mean”. The plyr package allows you to calculate a function over segments of your data and the zoo package has methods for rolling calculations.

    > lines <- "ID,Var2,Var3    
    + 1,A,1
    + 2,A,3
    + 3,A,5
    + 4,A,7
    + 5,A,9
    + 6,A,11
    + 7,B,2
    + 8,B,4
    + 9,B,6
    + 10,B,8
    + 11,B,10"
    > 
    > x <- read.csv(con <- textConnection(lines))
    > close(con)
    > 
    > ddply(x,"Var2",function(y) data.frame(y,
    +   mean=rollmean(y$Var3,3,na.pad=TRUE,align="right")))
       ID Var2 Var3 mean
    1   1    A    1   NA
    2   2    A    3   NA
    3   3    A    5    3
    4   4    A    7    5
    5   5    A    9    7
    6   6    A   11    9
    7   7    B    2   NA
    8   8    B    4   NA
    9   9    B    6    4
    10 10    B    8    6
    11 11    B   10    8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data set that I need to calculate the trait average of
I have a set of 'dynamic data' that I need to bind to the
I have a set of data protected by 16bit checksums that I need to
I have a set of data that contains garbled text fields because of encoding
I have a set of data that is structured like this: ItemA.GroupA ItemB.GroupA ItemC.GroupB
I have a data set that is around 700 rows with eight columns of
I have a data set that that I would like to call in a
I have set up a Core Data model that includes an entity, Item with
I have the following data set that I am trying to plot with ggplot2,
I have a large data set that I'm working with in excel. About 1000+

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.