Grouping and calculating averages

Hi,
I have a data used to monitor school performance. I have 4 variables here. I have grouped and summarized it as given below. But I want to add another layer where I can get the cumulative average based on the variable 'week'. For example, for week 2, the average of week 1 and 2 has to be shown.

``````library(tidyverse)

data<-tibble::tribble(
~master,    ~teacher, ~week, ~score,
"Anil",    "Gandhi",    1L,    19L,
"Nagma",     "Patel",    1L,    41L,
"Anil",     "Obama",    1L,    21L,
"Nagma",    "Gerard",    1L,    18L,
"Anil",     "Nixon",    1L,    26L,
"Nagma",     "Truss",    1L,    32L,
"Anil",    "Gandhi",    2L,    24L,
"Nagma",     "Patel",    2L,    43L,
"Anil",     "Obama",    2L,    20L,
"Nagma",    "Gerard",    2L,    13L,
"Anil",     "Nixon",    2L,    23L,
"Nagma",     "Truss",    2L,    14L,
"Anil",    "Gandhi",    3L,    32L,
"Nagma",     "Patel",    3L,    15L,
"Anil",     "Obama",    3L,    39L,
"Nagma",    "Gerard",    3L,    29L,
"Anil",     "Nixon",    3L,    38L,
"Nagma",     "Truss",    3L,    24L
)

group_dta<-data %>%
group_by(master,teacher,week) %>%
summarise(avg_score=mean(score))
#> `summarise()` has grouped output by 'master', 'teacher'. You can override using the `.groups` argument.
Created on 2022-09-23 by the reprex package (v2.0.1)
``````
``````(gdta2 <- mutate(group_dta,
cumavg = cummean(avg_score)))``````

Thank you very much.

Regards,
Nithin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.