Skip to content

Instantly share code, notes, and snippets.

@francisbarton
Last active February 11, 2020 04:21
Show Gist options
  • Save francisbarton/3c9f755a7f17ce5624edb9d4da0f4f59 to your computer and use it in GitHub Desktop.
Save francisbarton/3c9f755a7f17ce5624edb9d4da0f4f59 to your computer and use it in GitHub Desktop.
trying to make map* work with mutate to make new df columns from a list
# load packages -----------------------------------------------------------
library(rlang)
library(dplyr)
library(tidyr)
library(magrittr)
library(purrr)
library(nomisr)
# set up initial list of tibbles ------------------------------------------
df <- list(
district_population = tibble(
dataset_title = "Population estimates - local authority based by single year",
dataset_id = "NM_2002_1"
),
jsa_claimants = tibble(
dataset_title = "Jobseeker\'s Allowance with rates and proportions",
dataset_id = "NM_1_1"
)
)
# just use the first tibble for now, for testing --------------------------
# ideally I want to map across dfs through a list -------------------------
df <- df[[1]]
# generic add_columns function --------------------------------------------
nomis_add_columns <- function(dataset, new_column, function_name, these_vars_in_particular) {
dataset %>%
# map doesn't work if there's just one dataframe instead of a list
# map(~ mutate(., {{ new_column }} := function_name(!!!these_vars_in_particular)))
mutate(., {{ new_column }} := function_name(!!!these_vars_in_particular))
}
# add 'geography' column function -----------------------------------------
get_geog_concepts <- function(dataset_id) {
nomis_overview(id = dataset_id,
select = c("dimensions", "codes")) %>%
pluck("value", 1, "dimension") %>%
filter(concept == "geography") %>%
pluck("types.type") %>%
# if tibble already hds a geog_level column then this table would be
# filtered by that:
# filter(name == geog_level) %>%
as.list # required to obtain length = 1
}
# apply geography add column function -------------------------------------
# df %<>% nomis_add_columns(.,
# geography_types,
# get_geog_concepts,
# these_vars_in_particular = vars(dataset_id))
dput(df)
# structure(list(dataset_title = "Population estimates - local authority based by single year",
# dataset_id = "NM_2002_1", concepts = list(c("time", "gender",
# "c_age", "measures"))),
# class = c("tbl_df", "tbl", "data.frame"),
# row.names = c(NA, -1L))
# nitty gritty functions --------------------------------------------------
get_concept_list <- function(df) {
dataset_id <- pluck(df, "dataset_id")
nomis_overview(id = dataset_id,
select = c("dimensions", "codes")) %>%
pluck("value", 1, "dimension") %>%
filter(!concept == "geography") %>%
pull("concept")
}
get_concept_list(df)
# using toupper() here for simplicity - ultimately need to use
# get_concept_info()
get_concept_list(df) %>%
map(~ mutate(df, {{.x}} := toupper(.x)))
get_concept_list(df) %>%
map_chr(~ mutate(df, {{.x}} := toupper(.x)))
get_concept_list(df) %>%
map_df(~ mutate(df, {{.x}} := toupper(.x)))
get_concept_list(df) %>%
map_dfr(~ mutate(df, {{.x}} := toupper(.x)))
get_concept_list(df) %>%
map_dfc(~ mutate(df, {{.x}} := toupper(.x)))
# function to get info on each concept (except geography) -----------------
get_concept_info <- function(df, concept_name) {
dataset_id <- pluck(df, "dataset_id")
nomis_overview(id = dataset_id) %>%
filter(name == "dimensions") %>%
pluck("value", 1, "dimension") %>%
filter(concept == concept_name) %>%
pluck("codes.code", 1) %>%
select(name, value) %>%
nest(data = everything()) %>%
as.list() %>%
pluck("data")
}
# individual mutate works, for comparison ---------------------------------
# df %>% map(~ mutate(., time = get_concept_info(., concept_name = "time")))
df %>% mutate(., time = get_concept_info(df, "time"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment