Or you can simply calculate it using filter, here’s the function I use:
ma <- function(x, n = 5){filter(x, rep(1 / n, n), sides = 2)}
If you use dplyr
, be careful to specify stats::filter
in the function above.
More Related Contents:
- Moving average of previous three values in R
- r – How to find value with condition
- Reshaping data.frame from wide to long format
- Combine two data frames by rows (rbind) when they have different sets of columns
- Does ifelse really calculate both of its vectors every time? Is it slow?
- Selecting data frame rows based on partial string match in a column
- Reorder bars in geom_bar ggplot2 by value
- What is the purpose of setting a key in data.table?
- Filter data frame by character column name (in dplyr)
- Combine two or more columns in a dataframe into a new column with a new name
- How to generate a number of most distinctive colors in R?
- Keeping trailing zeros
- Bar plot with log scales
- R Loop for Variable Names to run linear regression model
- detach all packages while working in R
- How to change order of boxplots when using ggplot2?
- Using stargazer with Rstudio and Knitr
- listing contents of an R data file without loading
- R: Plotting a 3D surface from x, y, z
- How to prevent scientific notation in R? [duplicate]
- For the same code, labels (q1, median) appear on one computer but don’t appear on another computer
- How to reverse order a vector?
- Subset data.frame by date
- How to load data quickly into R?
- R divide data into groups
- Creating a vertical color gradient for a geom_bar plot
- Divide each value by the sum of values by group
- Dynamic linking with rpath not working under Ubuntu 17.10
- How to pass column name as argument to function for dplyr verbs?
- How to use data.table within functions and loops?