calc_summary_df: Calculate summary metrics on an organisation

Description Usage Arguments Value Examples

Description

Given individual level variables and an organisation structure, this function calculates aggregated metrics using either the cumulative approach (all individuals in that unit or its descendents) or the orthodox approach (individuals immediately associated with that unit only).

Usage

1
2
calc_summary_df(tg, df, tall_df = NULL, selected_vars,
  is_cumulative = FALSE)

Arguments

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 and (and optionally individual level variables) with one row per individual - see the indiv_df format in orgsurveyr-data-formats

tall_df

a data frame with columns named individual id, metric_id and value with multiple rows per individual - see the indiv_tall_df format in orgsurveyr-data-formats

selected_vars

names of variables in df or tall_df (character vector)

is_cumulative

whether to calculate cumulative or orthodox aggregations (logical)

Value

A tall data frame with three columns: unit_id, metric_id and value.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
library(tidygraph)
library(dplyr)
set.seed(1231)
tg1b <- create_realistic_org(4,3, prob=0.3, delete_units = TRUE) %>% simulate_unit_size

## Not run: 
plot_org(tg1b) + geom_node_text(aes(label=unit_size), color='white')

## End(Not run)
tg1b_indiv_df <- tg1b %>%
  simulate_individuals_df() %>%
  mutate(test_var2 = purrr::map_dbl(individual_id, ~rnorm(1, 20,3)))
tg1b_indiv_df

tg1b_indiv_tall_df <- tg1b_indiv_df %>%
  select(individual_id, test_var, test_var2) %>%
  tidyr::gather('metric_id', 'value', -individual_id)
tg1b_indiv_tall_df

# using wide data frame
calc_summary_df(tg1b, tg1b_indiv_df, NULL,
                'test_var2', is_cumulative=TRUE)
calc_summary_df(tg1b, tg1b_indiv_df, NULL,
                c('test_var', 'test_var2'), is_cumulative=TRUE)
calc_summary_df(tg1b, tg1b_indiv_df, NULL,
               'test_var2', is_cumulative=FALSE)
calc_summary_df(tg1b, tg1b_indiv_df, NULL,
                c('test_var', 'test_var2'), is_cumulative=FALSE)

# using tall data frame
calc_summary_df(tg1b, tg1b_indiv_df, tg1b_indiv_tall_df,
                'test_var2', is_cumulative=TRUE)
calc_summary_df(tg1b, tg1b_indiv_df, tg1b_indiv_tall_df,
                c('test_var', 'test_var2'), is_cumulative=TRUE)
calc_summary_df(tg1b, tg1b_indiv_df, tg1b_indiv_tall_df,
                'test_var2', is_cumulative=FALSE)
calc_summary_df(tg1b, tg1b_indiv_df, tg1b_indiv_tall_df,
                c('test_var', 'test_var2'), is_cumulative=FALSE)

ukgovdatascience/orgsurveyr documentation built on May 4, 2019, 7:41 p.m.