Skip to content

Instantly share code, notes, and snippets.

@stemangiola
Last active June 7, 2019 05:54
Show Gist options
  • Save stemangiola/9d2ba5d599b7ac80404c753cdee04a01 to your computer and use it in GitHub Desktop.
Save stemangiola/9d2ba5d599b7ac80404c753cdee04a01 to your computer and use it in GitHub Desktop.
tidy_data_tree
ToDataFrameTypeColFull = function(tree, ...){
tree %>%
Clone() %>%
{
t = (.)
foreach(l=1:(t %$% Get("level") %>% max), .combine = bind_rows) %do% {
data.tree::Clone(t) %>%
{ data.tree::Prune(., function(x) x$level <= l + 1); . } %>%
data.tree::ToDataFrameTypeCol(...) %>%
as_tibble
}
} %>%
distinct() %>%
{ if("level_3" %in% ((.) %>% colnames)) (.) %>% mutate(level_3 = ifelse(level_3 %>% is.na, level_2, level_3)) else (.) } %>%
{ if("level_4" %in% ((.) %>% colnames)) (.) %>% mutate(level_4 = ifelse(level_4 %>% is.na, level_3, level_4)) else (.) } %>%
{ if("level_5" %in% ((.) %>% colnames)) (.) %>% mutate(level_5 = ifelse(level_5 %>% is.na, level_4, level_5)) else (.) } %>%
{ if("level_6" %in% ((.) %>% colnames)) (.) %>% mutate(level_6 = ifelse(level_6 %>% is.na, level_5, level_6)) else (.) } %>%
select(..., everything())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment