# Calculating average based on selecting observations

Hi,
I have a dataset for tracking teachers. I need to check whether they are adopting a particular teaching practice (which is encoded as a categorical variable-1 or 0).
I need to calculate the average. The week number for a particular teacher appears 5 times in a week as classes happen 5 times. But the average has to be calculated teacher-wise. For example, if we take teacher Nithin in the reprex below, the average must be calculated as sum of the variable "part1" divided by 5. The same number must appear for all the days in that particular week. For week 2 again the calculation must be the same. This has to be calculated for all the teachers in the list. How can I do it. The resulting data frame will look like what is shown in "data2". How can I do this?

``````library(tidyverse)

data<-tibble::tribble(
~master,         ~schoolid,  ~teacherid, ~week_no, ~part1,
"A",      "GHPS UNKAL",     "Nitin",       1L,     0L,
"A",      "GHPS UNKAL",     "Savio",       1L,     1L,
"B", "GHPS VIDYANAGAR", "Prathibha",       1L,     0L,
"B", "GHPS VIDYANAGAR",     "Nagma",       1L,     1L,
"A",      "GHPS UNKAL",     "Nitin",       1L,     0L,
"A",      "GHPS UNKAL",     "Savio",       1L,     1L,
"B", "GHPS VIDYANAGAR", "Prathibha",       1L,     0L,
"B", "GHPS VIDYANAGAR",     "Nagma",       1L,     1L,
"A",      "GHPS UNKAL",     "Nitin",       1L,     0L,
"A",      "GHPS UNKAL",     "Savio",       1L,     1L,
"B", "GHPS VIDYANAGAR", "Prathibha",       1L,     0L,
"B", "GHPS VIDYANAGAR",     "Nagma",       1L,     1L,
"A",      "GHPS UNKAL",     "Nitin",       1L,     0L,
"A",      "GHPS UNKAL",     "Savio",       1L,     1L,
"B", "GHPS VIDYANAGAR", "Prathibha",       1L,     0L,
"B", "GHPS VIDYANAGAR",     "Nagma",       1L,     1L,
"A",      "GHPS UNKAL",     "Nitin",       1L,     0L,
"A",      "GHPS UNKAL",     "Savio",       1L,     1L,
"B", "GHPS VIDYANAGAR", "Prathibha",       1L,     0L,
"B", "GHPS VIDYANAGAR",     "Nagma",       1L,     1L,
"A",      "GHPS UNKAL",     "Nitin",       2L,     0L,
"A",      "GHPS UNKAL",     "Savio",       2L,     1L,
"B", "GHPS VIDYANAGAR", "Prathibha",       2L,     0L,
"B", "GHPS VIDYANAGAR",     "Nagma",       2L,     1L,
"A",      "GHPS UNKAL",     "Nitin",       2L,     0L,
"A",      "GHPS UNKAL",     "Savio",       2L,     1L,
"B", "GHPS VIDYANAGAR", "Prathibha",       2L,     0L,
"B", "GHPS VIDYANAGAR",     "Nagma",       2L,     1L,
"A",      "GHPS UNKAL",     "Nitin",       2L,     0L,
"A",      "GHPS UNKAL",     "Savio",       2L,     1L,
"B", "GHPS VIDYANAGAR", "Prathibha",       2L,     0L,
"B", "GHPS VIDYANAGAR",     "Nagma",       2L,     1L,
"A",      "GHPS UNKAL",     "Nitin",       2L,     0L,
"A",      "GHPS UNKAL",     "Savio",       2L,     1L,
"B", "GHPS VIDYANAGAR", "Prathibha",       2L,     0L,
"B", "GHPS VIDYANAGAR",     "Nagma",       2L,     1L,
"A",      "GHPS UNKAL",     "Nitin",       2L,     0L,
"A",      "GHPS UNKAL",     "Savio",       2L,     1L,
"B", "GHPS VIDYANAGAR", "Prathibha",       2L,     0L,
"B", "GHPS VIDYANAGAR",     "Nagma",       2L,     1L
)

data2<-tibble::tribble(
~master,         ~schoolid,  ~teacherid, ~week_no, ~part1, ~average, ~average_man_day,
"A",      "GHPS UNKAL",     "Nitin",       1L,     0L,       0L,                0,
"A",      "GHPS UNKAL",     "Savio",       1L,     1L,       1L,             1.25,
"B", "GHPS VIDYANAGAR", "Prathibha",       1L,     0L,       0L,                0,
"B", "GHPS VIDYANAGAR",     "Nagma",       1L,     1L,       1L,             1.25,
"A",      "GHPS UNKAL",     "Nitin",       1L,     0L,       0L,                0,
"A",      "GHPS UNKAL",     "Savio",       1L,     1L,       1L,             1.25,
"B", "GHPS VIDYANAGAR", "Prathibha",       1L,     0L,       0L,                0,
"B", "GHPS VIDYANAGAR",     "Nagma",       1L,     1L,       1L,             1.25,
"A",      "GHPS UNKAL",     "Nitin",       1L,     0L,       0L,                0,
"A",      "GHPS UNKAL",     "Savio",       1L,     1L,       1L,             1.25,
"B", "GHPS VIDYANAGAR", "Prathibha",       1L,     0L,       0L,                0,
"B", "GHPS VIDYANAGAR",     "Nagma",       1L,     1L,       1L,             1.25,
"A",      "GHPS UNKAL",     "Nitin",       1L,     0L,       0L,                0,
"A",      "GHPS UNKAL",     "Savio",       1L,     1L,       1L,             1.25,
"B", "GHPS VIDYANAGAR", "Prathibha",       1L,     0L,       0L,                0,
"B", "GHPS VIDYANAGAR",     "Nagma",       1L,     1L,       1L,             1.25,
"A",      "GHPS UNKAL",     "Nitin",       1L,     0L,       0L,                0,
"A",      "GHPS UNKAL",     "Savio",       1L,     1L,       1L,             1.25,
"B", "GHPS VIDYANAGAR", "Prathibha",       1L,     0L,       0L,                0,
"B", "GHPS VIDYANAGAR",     "Nagma",       1L,     1L,       1L,             1.25,
"A",      "GHPS UNKAL",     "Nitin",       2L,     0L,       0L,                0,
"A",      "GHPS UNKAL",     "Savio",       2L,     1L,       1L,             1.25,
"B", "GHPS VIDYANAGAR", "Prathibha",       2L,     0L,       0L,                0,
"B", "GHPS VIDYANAGAR",     "Nagma",       2L,     1L,       1L,             1.25,
"A",      "GHPS UNKAL",     "Nitin",       2L,     0L,       0L,                0,
"A",      "GHPS UNKAL",     "Savio",       2L,     1L,       1L,             1.25,
"B", "GHPS VIDYANAGAR", "Prathibha",       2L,     0L,       0L,                0,
"B", "GHPS VIDYANAGAR",     "Nagma",       2L,     1L,       1L,             1.25,
"A",      "GHPS UNKAL",     "Nitin",       2L,     0L,       0L,                0,
"A",      "GHPS UNKAL",     "Savio",       2L,     1L,       1L,             1.25,
"B", "GHPS VIDYANAGAR", "Prathibha",       2L,     0L,       0L,                0,
"B", "GHPS VIDYANAGAR",     "Nagma",       2L,     1L,       1L,             1.25,
"A",      "GHPS UNKAL",     "Nitin",       2L,     0L,       0L,                0,
"A",      "GHPS UNKAL",     "Savio",       2L,     1L,       1L,             1.25,
"B", "GHPS VIDYANAGAR", "Prathibha",       2L,     0L,       0L,                0,
"B", "GHPS VIDYANAGAR",     "Nagma",       2L,     1L,       1L,             1.25,
"A",      "GHPS UNKAL",     "Nitin",       2L,     0L,       0L,                0,
"A",      "GHPS UNKAL",     "Savio",       2L,     1L,       1L,             1.25,
"B", "GHPS VIDYANAGAR", "Prathibha",       2L,     0L,       0L,                0,
"B", "GHPS VIDYANAGAR",     "Nagma",       2L,     1L,       1L,             1.25
)
Created on 2022-10-19 by the reprex package (v2.0.1)
``````

Hello,

doing the grouped average should be no concern. You can just use

``````library('dplyr')
data |>
group_by(week_no, teacherid) |>
mutate(average = mean(part1))
``````

But how is average_man_day even calculated? Maybe I am misreading something, but I cannot see any information in your request about this specific column. Maybe you could clarify it in a bit more detail?

Kind regards

Thanks a lot.. This code works well for me. The variable average_man_day is calculated as the average divided by the number of teachers multiplied by number of classes a week (we have 4 teachers and 5 classes a week). For that I used the following code:

``````data %>%
group_by(teacherid,week_no) %>%
mutate(avg=mean(part1)) %>%
rowwise() %>%
mutate(average_man_day=avg/(4*5))
``````

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.