Suppose I have a list of named vectors:
test_list <- list("grp1" = c(subgrp1 = "A", subgrp2 = "B"),
"grp2" = c(subgrp1 = "A", subgrp2 = "B"),
"grp3" = c(subgrp1 = "A"))
I want to change this into
want_list <- list("grp1 - subgrp1" = c(subgrp1 = "A"),
"grp1 - subgrp2" = c(subgrp2 = "B"),
"grp2 - subgrp1" = c(subgrp1 = "A"),
"grp2 - subgrp2" = c(subgrp2 = "B"),
"grp3" = c(subgrp1 = "A"))
purrr::flatten(test_list) returns list of length 5, but loses the "grp" hierarchy completely - which isn't surprising since that's the point of this function. I wish to "maintain" the hierarchy by keeping the name on those with length of >1.
I suppose you can rename each elements in named vector based on the layer above... but I am not sure how to do this operation in scalable manner when the list has varying length.
How can I approach this problem?