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
  • 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-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

We have this set of data that we need to get the average of
I have a set of data protected by 16bit checksums that I need to
I have a set of data that models a hierarchy of categories. A root
I have a set of data that needs to be displayed as a crosstab
I have a data set that is organized in the following manner: Timestamp|A0001|A0002|A0003|A0004|B0001|B0002|B0003|B0004 ...
I am working with a set of data that I have converted to a
I have a costumer showing Notepad with a large set of data that looks
I have a set of core, complicated JavaScript data structures/classes that I'd like to
So I have made a webservice that interfaces with a set of data contained
so i have a winforms apps that downloads a set of data syncronously on

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.