R/generate_cumulative_mapping_df.R
generate_cumulative_mapping.Rd
Takes an organisation as a tbl_graph object and a data frame that maps individuals to units and returns a data frame that maps an organisational unit to all individuals within that unit.
generate_cumulative_mapping(tg, df)
tg | tbl_graph that passes a check with check_tbl_graph_is_org |
---|---|
df | a data frame with columns named unit_id and invididual_id - see see the indiv_df format
in |
tbl_df
For example, if an organisation is split into four departments, and each department has four teams, an employee is affiliated not just to their immediate team, but also their department and the organisation as a whole.
The output dataframe serves as a basis for calculating unit level summaries that include all individuals affiliated to that unit.
library(dplyr) set.seed(1234) tg_ex1 <- create_realistic_org(n_children = 4, max_depth = 3, prob=0.3) tg_ex1 <- simulate_unit_size(tg_ex1) df <- simulate_individuals_df(tg_ex1) map_df <- generate_cumulative_mapping(tg_ex1, df) map_df#> # A tibble: 552 x 2 #> parent_id individual_id #> <chr> <chr> #> 1 1 1 #> 2 1 2 #> 3 1 3 #> 4 1 4 #> 5 1 5 #> 6 1 6 #> 7 1 7 #> 8 1 8 #> 9 1 9 #> 10 1 10 #> # ... with 542 more rows#use the map to calculate cumulative means df %>% inner_join(map_df, by='individual_id') %>% group_by(parent_id) %>% summarise(cum_test_var = mean(test_var), cum_n = n())#> # A tibble: 42 x 3 #> parent_id cum_test_var cum_n #> <chr> <dbl> <int> #> 1 1 10.2 146 #> 2 10 9.80 10 #> 3 11 10.7 20 #> 4 12 9.95 21 #> 5 13 9.24 12 #> 6 15 12.0 6 #> 7 17 10.6 13 #> 8 2 10.2 59 #> 9 22 11.1 5 #> 10 23 9.58 5 #> # ... with 32 more rows