How to Calculate a Cumulative Average in R
Cumulative Average in R, The average of a series of values up to a certain point is given by a cumulative average.
In R, you can calculate the cumulative average of values using the following methods.
Approach 1:- Base R
cum_avg <- cumsum(x) / seq_along(x)
Approach 2:- dplyr
library(dplyr) cum_avg <- cummean(x)
Approach 3-: Using cumsum and seq_along
data$cum_avg_volume<-cumsum(data$Volume) / seq_along(data$Volume)
How to Calculate Jaccard Similarity in R » finnstats
Above techniques get the same output, although the dplyr method is faster when working with huge data frames.
With the following data frame in R, the following examples explain how to utilize each method in practice.
Let’s create a data frame.
data <- data.frame(Month=seq(1:12), Volume=c(3,5,7,8,4,9,1,2,9,12,14,15))
Now we can view the head of the data frame.
Granger Causality Test in R (with Example) » finnstats
head(data)
Month Volume 1 1 3 2 2 5 3 3 7 4 4 8 5 5 4 6 6 9
Approach 1: Using Base R, calculate the Cumulative Average.
data$cum_avg_volume <- cumsum(data$Volume) / seq_along(data$Volume)
Okay, now view the updated data frame.
String Manipulation in R » finnstats
data
Month Volume cum_avg_volume 1 1 3 3.000000 2 2 5 4.000000 3 3 7 5.000000 4 4 8 5.750000 5 5 4 5.400000 6 6 9 6.000000 7 7 1 5.285714 8 8 2 4.875000 9 9 9 5.333333 10 10 12 6.000000 11 11 14 6.727273 12 12 15 7.416667
The cumulative average values could be interpreted as follows:
The first sales value’s cumulative average is 3.
The first two sales values have a cumulative average of 4.
The first three sales values have a cumulative average of 5.
And so forth.
What is neural network in machine learning? » finnstats
Example 2: Using dplyr, calculate the cumulative average
To calculate a cumulative average in R, we may use the cummean function from the dplyr package.
The following code demonstrates how to use this function to add a new column to our data frame that represents the volume cumulative average:
library(dplyr)
add a new column with the cumulative average of volume.
Regression Analysis Example-Ultimate Guide » finnstats
data <- data.frame(Month=seq(1:12), Volume=c(3,5,7,8,4,9,1,2,9,12,14,15))
Let’s view the updated data frame
data
Month Volume cum_avg_volume 1 1 3 3.000000 2 2 5 4.000000 3 3 7 5.000000 4 4 8 5.750000 5 5 4 5.400000 6 6 9 6.000000 7 7 1 5.285714 8 8 2 4.875000 9 9 9 5.333333 10 10 12 6.000000 11 11 14 6.727273 12 12 15 7.416667
It’s worth noting that this strategy produces the same outcomes as the prior one.
Example 3: Using cumsum and seq_along
Using the cumsum and seq_along functions from the R programming language.
Cross Validation in R with Example » finnstats
data <- data.frame(Month=seq(1:12), Volume=c(3,5,7,8,4,9,1,2,9,12,14,15)) data$cum_avg_volume<-cumsum(data$Volume) / seq_along(data$Volume) data
Month Volume cum_avg_volume 1 1 3 3.000000 2 2 5 4.000000 3 3 7 5.000000 4 4 8 5.750000 5 5 4 5.400000 6 6 9 6.000000 7 7 1 5.285714 8 8 2 4.875000 9 9 9 5.333333 10 10 12 6.000000 11 11 14 6.727273 12 12 15 7.416667
Model Selection in R (AIC Vs BIC) » finnstats
It’s worth noting that this technique yields the same results as we have seen in the one and two.