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,
        "Madhumathi",     "Nehru",    1L,    27L,
             "Nagma",     "Patel",    1L,    41L,
              "Anil",     "Obama",    1L,    21L,
        "Madhumathi", "Roosevelt",    1L,    25L,
             "Nagma",    "Gerard",    1L,    18L,
              "Anil",     "Nixon",    1L,    26L,
        "Madhumathi",  "Thatcher",    1L,    45L,
             "Nagma",     "Truss",    1L,    32L,
              "Anil",    "Gandhi",    2L,    24L,
        "Madhumathi",     "Nehru",    2L,    12L,
             "Nagma",     "Patel",    2L,    43L,
              "Anil",     "Obama",    2L,    20L,
        "Madhumathi", "Roosevelt",    2L,    28L,
             "Nagma",    "Gerard",    2L,    13L,
              "Anil",     "Nixon",    2L,    23L,
        "Madhumathi",  "Thatcher",    2L,    43L,
             "Nagma",     "Truss",    2L,    14L,
              "Anil",    "Gandhi",    3L,    32L,
        "Madhumathi",     "Nehru",    3L,    19L,
             "Nagma",     "Patel",    3L,    15L,
              "Anil",     "Obama",    3L,    39L,
        "Madhumathi", "Roosevelt",    3L,    10L,
             "Nagma",    "Gerard",    3L,    29L,
              "Anil",     "Nixon",    3L,    38L,
        "Madhumathi",  "Thatcher",    3L,    32L,
             "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.