Selection of particular variables and calculating ratio

Hi,
I have a dataset of student attendance as given below. The attendance response begins from q_1_1 till q_10_1. Different schools have different number of students. I have to calculate the attendance ratio (students attended/total students in that school). I have tried doing it, but it does not give me the desired result (it calculates all the "1" that comes its way). The problem is I do not want to create another data frame consisting of just attendance data. I want to calculate the ratio using the original data frame.

library(tidyverse)

data<-tibble::tribble(
  ~enumerator, ~instr_name,           ~school,    ~teacher, ~tchr_attend, ~teacher_support, ~q_1_1, ~q_2_1, ~q_3_1, ~q_4_1, ~q_5_1, ~q_6_1, ~q_7_1, ~q_8_1, ~q_9_1, ~q_10_1,
     "BEN001",    "Nithin",    "GHPS KUSUGAL",  "Rajmohan",           2L,             0.25,     1L,     0L,     1L,     0L,     1L,     0L,     0L,     NA,     NA,      NA,
     "BEN002",    "Gandhi",    "GHPS KUSUGAL",     "David",           1L,             0.75,     0L,     0L,     1L,     0L,     0L,     1L,     1L,     0L,     1L,      NA,
     "BEN003",     "Nehru",  "GHPS YARIKOPPA",     "Muthu",           1L,              0.5,     1L,     1L,     0L,     0L,     0L,     0L,     0L,     0L,     1L,      1L,
     "BEN004",     "Patel",    "HPS HEBBALLI",     "Madhu",           1L,             0.45,     0L,     1L,     1L,     1L,     0L,     0L,     0L,     0L,     0L,      0L,
     "BEN005",     "Devan",    "GHPS BELLARY",     "Nagma",           2L,             0.75,     1L,     0L,     0L,     0L,     1L,     NA,     NA,     NA,     NA,      NA,
     "BEN006",     "Mohan",     "GHPS KANNUR",     "Bindu",           2L,             0.25,     0L,     1L,     0L,     1L,     1L,     0L,     0L,     0L,     1L,      0L,
     "BEN007",       "Lal",      "GHPS HUBLI", "Prathibha",           1L,             0.75,     0L,     1L,     1L,     1L,     1L,     0L,     1L,     0L,     NA,      NA,
     "BEN008",  "Srinivas", "GHPS VIDYANAGAR",      "Ivan",           1L,              0.5,     0L,     0L,     1L,     0L,     1L,     0L,     0L,     NA,     NA,      NA,
     "BEN009",     "Vikas",    "GHPS HEGGERI",     "Zidan",           1L,             0.25,     1L,     0L,     0L,     0L,     0L,     0L,     0L,     1L,     0L,      1L,
     "BEN010",     "Navin",    "GHPS HEGGERI",     "Obama",           1L,             0.25,     1L,     0L,     0L,     1L,     0L,     1L,     0L,     1L,     1L,      NA
  )

mis<-data %>% 
  rowwise() %>% 
  mutate(
    attend=sum(across(where(is.numeric), ~.x==1),na.rm = T),
    NDenom=sum(across(where(is.numeric), ~!is.na(.x)),na.rm = T),
    attend_rat=(attend/NDenom)*100
  ) %>%
  ungroup() 
Created on 2022-09-19 by the reprex package (v2.0.1)
1 Like

Why ? How do you justify this ?

I actually wanted to make a shiny app with the data. I wanted all the filters in one data frame. My thinking was it was essential to have one frame to work with in a shiny app. Does this sound logical?

Theres nothing about shiny that implies a single frame, though your use case might. Even if you want a single frame to be a result shown to a user you are free to combine multiple frames and show the combined result.

Ok thank you for this. I think I can do this now. Combining was something I did not think about.