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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:09:15+00:00 2026-06-19T03:09:15+00:00

This line given below will simply calculate the moving average for certain values with

  • 0

This line given below will simply calculate the moving average for certain values with a window of 3. As the total number of values is 12 and the moving-average widow is 3, the number of resulted values is 10 (as shown below).

library(zoo)
x <- c(1,2,3,NA,NA,4,6,5,6,4,2,5)
movingmean <- rollapply(x, 3, FUN = mean, na.rm = T)
movingmean
#  [1] 2.000000 2.500000 3.000000 4.000000 5.000000 5.000000
#  [7] 5.666667 5.000000 4.000000 3.666667

I want to subtract these averages (movingmean) from the corresponding original value.

Example: 2-2.000000, 3-2.500000, NA-3.000000, NA-4.000000, ..., 2-3.666667.

  • 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-19T03:09:16+00:00Added an answer on June 19, 2026 at 3:09 am

    By default, rollapply does not pad the result with NA. Set fill=NA to do so.

    movingmean <- rollapply(x, 3, FUN=mean, na.rm=TRUE, fill=NA)
    x - movingmean
    #  [1]         NA  0.0000000  0.5000000         NA         NA
    #  [6] -1.0000000  1.0000000 -0.6666667  1.0000000  0.0000000
    # [11] -1.6666667         NA
    

    Also note that rollapply uses a centered window by default. You can change it via the align argument or use rollapplyr if you want a right-aligned calculation (as with most time series).

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

Sidebar

Related Questions

Given this CSS body { font-size: 62.5%; font-family: Arial,sans-serif; line-height: 2em; color:#333 } .post_text
Given I have a long phrase, say, in line :12 , as this example:
i understand that the code given below will not be compltely understood unless i
I have texts like this and the formats are given below. Salary is 3.6L
Currently the code below will give me a line by line listing of the
I was looking at this line: extensions << Module.new(&block) if block_given? It seems to
Is that normal that this line: echo date(Y-m-d h:m:s a, strtotime('2012-03-18 12:55:00')) gives me
ok I give up, how do you do this in one line? public object
This query gives me syntax error in when-between line. how can i solve them?
This Codes give me error: Msg 156, Level 15, State 1, Procedure DefaultActivity, Line

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.