Skip to content

Instantly share code, notes, and snippets.

@bhive01
Created March 2, 2020 18:50
Show Gist options
  • Save bhive01/499e94f6205ac1b5c2f26451211034ca to your computer and use it in GitHub Desktop.
Save bhive01/499e94f6205ac1b5c2f26451211034ca to your computer and use it in GitHub Desktop.
data <- dget("example_data.dput")
data_summary <-
function(df, V1, V2, V3, V4, value) {
V1_var <- enquo(V1)
V2_var <- enquo(V2)
V3_var <- enquo(V3)
V4_var <- enquo(V4)
layer_1 <- enquos(V1, V2, V3, V4)
layer_2 <- enquos(V1, V2, V4)
layer_3 <- enquos(V1, V4)
layer_4 <- enquos(V4)
value_var <- enquo(value)
V1_order <- df %>% pull(!!V1_var) %>% unique(.) %>% as.character()
V2_order <- df %>% pull(!!V2_var) %>% unique(.) %>% as.character()
V3_order <- df %>% pull(!!V3_var) %>% unique(.) %>% as.character()
V1_1 <- sym(V1_order[1])
V1_2 <- sym(V1_order[2])
V2_1 <- sym(V2_order[1])
V2_2 <- sym(V2_order[2])
V3_1 <- sym(V3_order[1])
V3_2 <- sym(V3_order[2])
df %>%
dplyr::group_by(!!!layer_1) %>%
dplyr::summarise(!!value_var := mean(!!value_var, na.rm = TRUE)) %>%
tidyr::pivot_wider(, names_from = !!V3_var, values_from = !!value_var) %>%
dplyr::mutate(!!value_var := !!V3_1 - !!V3_2) %>%
dplyr::select(-!!V3_1, -!!V3_2) %>%
tidyr::pivot_wider(, names_from = !!V2_var, values_from = !!value_var) %>%
dplyr::mutate(!!value_var := !!V2_1 - !!V2_2) %>%
dplyr::select(-!!V2_1, -!!V2_2) %>%
tidyr::pivot_wider(, names_from = !!V1_var, values_from = !!value_var) %>%
dplyr::mutate(!!value_var := !!V1_1 - !!V1_2) %>%
dplyr::select(-!!V1_1, -!!V1_2)
}
data_summary(data, toj_condition, block_type, target_type, sample, value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment