How can I calculate a cumulative average in R?

How can I calculate a cumulative average in R?

Calculating a cumulative average in R involves finding the average of a set of numbers at each step of the data. This is done by adding up all the values up to a certain point and dividing it by the total number of data points up to that point. This process is repeated for each subsequent data point, resulting in a running average. In R, this can be achieved by using the cummean() function from the base package or the cummean() function from the dplyr package. These functions simplify the process of calculating a cumulative average and provide accurate results. With the use of these functions, users can easily analyze and interpret their data sets to track trends and patterns over time.

Calculate a Cumulative Average in R


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 package in R to calculate a cumulative average.

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:

Cite this article

stats writer (2024). How can I calculate a cumulative average in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-calculate-a-cumulative-average-in-r/

stats writer. "How can I calculate a cumulative average in R?." PSYCHOLOGICAL SCALES, 30 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-calculate-a-cumulative-average-in-r/.

stats writer. "How can I calculate a cumulative average in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-calculate-a-cumulative-average-in-r/.

stats writer (2024) 'How can I calculate a cumulative average in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-calculate-a-cumulative-average-in-r/.

[1] stats writer, "How can I calculate a cumulative average in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I calculate a cumulative average in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top