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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:43:01+00:00 2026-06-12T15:43:01+00:00

Given a data frame with two columns, I’m looking to calculate a third column

  • 0

Given a data frame with two columns, I’m looking to calculate a third column which would contain the mean for every n number of rows while keeping the data frame intact.

Given the data frame

 index<-1:20
 V<-c(2,5,7,4,8,9,4,6,8,NA,3,4,5,6,0,4,5,7,5,3)
 DF<-data.frame(index,V)

How could I create DF$mean which would be the non-rolling mean of every 5 rows.

   index V  mean
      1 2    5.2
      2 5    5.2
      3 7    5.2
      4 4    5.2
      5 8    5.2
      6 9    6.75
      7 4    6.75    
      8 6    6.75    
      9 8    6.75    
     10 NA   6.75  
     11 3    3.6
     12 4    3.6
     13 5    3.6
     14 6    3.6 
     15 0    3.6
     16 4    4.8
     17 5    4.8
     18 7    4.8
     19 5    4.8
     20 3    4.8
  • 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-12T15:43:03+00:00Added an answer on June 12, 2026 at 3:43 pm
    DF$mean <- ave(DF$V, 
                   rep(1:(nrow(DF)/5), each=5), 
                   FUN=function(x){mean(x, na.rm=TRUE)})
    

    which gives

    > DF
       index  V mean
    1      1  2 5.20
    2      2  5 5.20
    3      3  7 5.20
    4      4  4 5.20
    5      5  8 5.20
    6      6  9 6.75
    7      7  4 6.75
    8      8  6 6.75
    9      9  8 6.75
    10    10 NA 6.75
    11    11  3 3.60
    12    12  4 3.60
    13    13  5 3.60
    14    14  6 3.60
    15    15  0 3.60
    16    16  4 4.80
    17    17  5 4.80
    18    18  7 4.80
    19    19  5 4.80
    20    20  3 4.80
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just learning R. Given a data.frame in R with two columns, one numeric and
I have a data frame which has two columns of dates in the format
Given two data frames: C1<-c(3,4,4,4,5) C2<-c(3,7,3,4,5) C3<-c(5,6,3,7,4) DF<-data.frame(C1=C1,C2=C2,C3=C3) DF C1 C2 C3 1 3
Given two data frames: df1 = data.frame(CustomerId = c(1:6), Product = c(rep(Toaster, 3), rep(Radio,
I have a data frame with two columns: an ID and some numeric value
for a given data frame I would like to multiply values of an array
I am working with data, 1st two columns are dates, 3rd column is symbol,
I have two data.frames in R, one of which has two columns and of
Given data frame values are Group year Value A 2010 17 A 2011 18
Given data like this C1<-c(3,-999.000,4,4,5) C2<-c(3,7,3,4,5) C3<-c(5,4,3,6,-999.000) DF<-data.frame(ID=c(A,B,C,D,E),C1=C1,C2=C2,C3=C3) How do I go about removing

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.