r - Recursive object-listing -
is there elegant way in r recursively turn arbitrary deep list (containing lists , vectors) vector of paths? example, convert this:
list( home = list( jerry = c("r", "bla"), mary = "xx" ), tmp = c("foo", "bar"), c("etc") ) to object this:
c( "/home/jerry/r", "/home/jerry/bla", "/home/mary/xx", "/tmp/foo", "/tmp/bar", "/etc" )
the names in unlist approximately want:
> test <- list( + home = list( + jerry = c("r", "bla"), + mary = "xx" + ), + tmp = c("foo", "bar"), + etc = c() + ) > unlist(test) home.jerry1 home.jerry2 home.mary tmp1 tmp2 "r" "bla" "xx" "foo" "bar" handles multiple levels of recursion well:
> test <- list( + home = list( + jerry = list(a="r", b="bla"), + mary = list(c="xx") + ), + tmp = list(d="foo", e="bar"), + etc = list(nothing=null) + ) > unlist(test) home.jerry.a home.jerry.b home.mary.c tmp.d tmp.e "r" "bla" "xx" "foo" "bar" from there it's easy add last little bit want (having final value last path elemtn) on:
> unl <- unlist(test) > res <- names(unl) > res <- paste(res,unl,sep=".") > res [1] "home.jerry.a.r" "home.jerry.b.bla" "home.mary.c.xx" "tmp.d.foo" "tmp.e.bar"
Comments
Post a Comment