A cumulative average tells us the average of a series of values up to a certain point.
You can use the following methods to calculate the cumulative average of values in R:
Method 1: Use Base R
cum_avg <- cumsum(x) / seq_along(x)
Method 2: Use dplyr
library(dplyr)
cum_avg <- cummean(x)
Both methods return the exact same result, but the dplyr method tends to work faster on large data frames.
The following examples show how to use each method in practice with the following data frame in R:
#create data frame df <- data.frame(day=seq(1:16), sales=c(3, 6, 0, 2, 4, 1, 0, 1, 4, 7, 3, 3, 8, 3, 5, 5)) #view head of data frame head(df) day sales 1 1 3 2 2 6 3 3 0 4 4 2 5 5 4 6 6 1
Example 1: Calculate Cumulative Average Using Base R
We can use the following code to add a new column to our data frame that shows the cumulative average of sales:
#add new column that contains cumulative avg. of sales
df$cum_avg_sales <- cumsum(df$sales) / seq_along(df$sales)
#view updated data frame
df
day sales cum_avg_sales
1 1 3 3.000000
2 2 6 4.500000
3 3 0 3.000000
4 4 2 2.750000
5 5 4 3.000000
6 6 1 2.666667
7 7 0 2.285714
8 8 1 2.125000
9 9 4 2.333333
10 10 7 2.800000
11 11 3 2.818182
12 12 3 2.833333
13 13 8 3.230769
14 14 3 3.214286
15 15 5 3.333333
16 16 5 3.437500
We would interpret the cumulative average values as:
- The cumulative average of the first sales value is 3.
- The cumulative average of the first two sales values is 4.5.
- The cumulative average of the first three sales values is 3.
- The cumulative average of the first four sales values is 2.75.
And so on.
Example 2: Calculate Cumulative Average Using dplyr
We can also use the cummean function from the dplyr package in R to calculate a cumulative average.
The following code shows how to use this function to add a new column to our data frame that shows the cumulative average of sales:
library(dplyr)
#add new column that contains cumulative avg. of sales
df$cum_avg_sales <- cummean(df$sales)
#view updated data frame
df
day sales cum_avg_sales
1 1 3 3.000000
2 2 6 4.500000
3 3 0 3.000000
4 4 2 2.750000
5 5 4 3.000000
6 6 1 2.666667
7 7 0 2.285714
8 8 1 2.125000
9 9 4 2.333333
10 10 7 2.800000
11 11 3 2.818182
12 12 3 2.833333
13 13 8 3.230769
14 14 3 3.214286
15 15 5 3.333333
16 16 5 3.437500
Notice that this method returns the exact same results as the previous method.
Additional Resources
The following tutorials explain how to calculate other common metrics in R:
How to Calculate a Trimmed Mean in R
How to Calculate Geometric Mean in R
How to Calculate a Weighted Mean in R