Home > Enterprise >  Adding a running average to a dataframe in R [duplicate]
Adding a running average to a dataframe in R [duplicate]

Time:09-23

I am trying to get the running average of a set of data at each data point. The data is set up as below:

    data <- data.frame(Exam = c(1:5),
                       Grade = c(90, 88, 95, 80, 82)

    data

    #   Exam    Grade
    1   1       90
    2   2       88
    3   3       95
    4   4       80
    5   5       82

Using dplyr, I am hoping to mutate the running average into a third column. The comments explain how the calculation I am looking for is done.

    #   Exam    Grade   RunningAvg
    1   1       90      90.00       # = 90 / 1
    2   2       88      89.00       # = (90   88) / 2
    3   3       95      91.00       # = (90   88   95) / 3
    4   4       80      88.25       # = (90   88   95   80) / 4
    5   5       82      87.00       # = (90   88   95   80   82) / 5

I have been trying to do this by summing the grades on a filter based on which number exam it is. My guess is that the code should look something like this, but I’m having no luck.

    data <- mutate(data, RunningAvg = cumsum(Grade[data$Exam <= Exam]) / Exam)

Any advice would be greatly appreciated!

CodePudding user response:

We may use cummean

library(dplyr)
data <- data %>%
     mutate(RunningAvg = cummean(Grade))

-output

data
Exam Grade RunningAvg
1    1    90      90.00
2    2    88      89.00
3    3    95      91.00
4    4    80      88.25
5    5    82      87.00

CodePudding user response:

In base R:

within(data, RunningAvg <- sapply(1:nrow(data), \(x) mean(Grade[1:x])))
  • Related